* @license LGPL, see license.txt for details * @link http://www.php-tools.net */ class patForms_Rule_RegexReplace extends patForms_Rule { /** * name of the rule * * @abstract * @access private */ var $ruleName = 'RegexReplace'; /** * define error codes and messages for the rule * * @access private * @var array $validatorErrorCodes * @todo translate error messages */ var $validatorErrorCodes = array( 'C' => array( 1 => 'This field contains invalid characters.', 2 => 'Invalid characters where automatically replaced - please verify the field.' ), 'de' => array( 1 => 'Die Eingabe enthält ungültige Zeichen.', 2 => 'Die ungültigen Zeichen wurden ersetzt - Bitte überprüfen sie den neuen Wert.' ) ); /** * perl compatiple regular expression * * @var string * @access private */ var $_regex = array( '/[\\W_]/', '_' ); /** * flag to indicate whether actual value should be replaced for real * * @var boolean * @access private */ var $_replace = true; /** * flag to indicate whether replace results in a form error * * @var boolean * @access private * @see $_replace */ var $_replaceError = true; /** * set regular expression * * @access public * @param string $search * @param string $replace * @param boolean $flag */ function setRegex( $search, $replace, $flag = true ) { $this->_regex = array( $search, $replace ); $this->_replace = $flag; } /** * enable display replace error * * @access public * @param boolean */ function enableReplace( $replace = true, $error = true ) { $this->_replaceError = $error; $this->_replace = $replace; } /** * method called by patForms or any patForms_Element to validate the * element or the form. * * @access public * @param object patForms form object */ function applyRule( &$element, $type = PATFORMS_RULE_BEFORE_VALIDATION ) { $value = $element->getValue(); if (empty($value)) { return true; } $replace = preg_replace( $this->_regex[0], $this->_regex[1], $value ); if( $replace != $value ) { if( !$this->_replace ) { $this->addValidationError( 1 ); } if( $this->_replace ) { $element->setValue( $replace ); if( !$this->_replaceError ) { return true; } $this->addValidationError( 2 ); return false; } return true; } return true; } } ?>