* @license LGPL, see license.txt for details * @link http://www.php-tools.net */ class patForms_Rule_Match extends WBPAT_Forms_Rule { /** * default config values * - regex * - match 1 * * @var array */ protected $config = array( 'reges' => '', 'match' => '1', ); /** * Initialize validation codes using gettext * * Overwrite this function to add element's error codes * * @access protected * @return bool $success Always returns true. * @see $attributeDefaults */ function loadValidatiorErrorCodes() { $this->validatorErrorCodes = array( 1 => patI18n::dgettext('wombat', 'Value is invalid'), ); return true; } /** * 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(false); if (empty($value)) { return true; } $match = intval($this->config['match']); $result = preg_match($this->config['regex'], $value); // matches but is not supposed to do so if ($result && 1 > $match) { $this->addValidationError(1); return false; } // does not match, but is supposed to match if (!$result && 0 < $match) { $this->addValidationError(1); return false; } return true; } } ?>