| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
class sfRegexValidator extends sfValidator |
|---|
| 36 |
{ |
|---|
| 37 |
|
|---|
| 38 |
* Executes this validator. |
|---|
| 39 |
* |
|---|
| 40 |
* @param string A parameter value |
|---|
| 41 |
* @param string An error message reference |
|---|
| 42 |
* |
|---|
| 43 |
* @return bool true, if this validator executes successfully, otherwise false |
|---|
| 44 |
*/ |
|---|
| 45 |
public function execute(&$value, &$error) |
|---|
| 46 |
{ |
|---|
| 47 |
$match = $this->getParameterHolder()->get('match'); |
|---|
| 48 |
$pattern = $this->getParameterHolder()->get('pattern'); |
|---|
| 49 |
|
|---|
| 50 |
if (($match && !preg_match($pattern, $value)) || |
|---|
| 51 |
(!$match && preg_match($pattern, $value))) |
|---|
| 52 |
{ |
|---|
| 53 |
$error = $this->getParameterHolder()->get('match_error'); |
|---|
| 54 |
|
|---|
| 55 |
return false; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
return true; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
* Initializes this validator. |
|---|
| 63 |
* |
|---|
| 64 |
* @param sfContext The current application context |
|---|
| 65 |
* @param array An associative array of initialization parameters |
|---|
| 66 |
* |
|---|
| 67 |
* @return bool true, if initialization completes successfully, otherwise false |
|---|
| 68 |
*/ |
|---|
| 69 |
public function initialize($context, $parameters = null) |
|---|
| 70 |
{ |
|---|
| 71 |
|
|---|
| 72 |
parent::initialize($context); |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
$this->getParameterHolder()->set('match', true); |
|---|
| 76 |
$this->getParameterHolder()->set('match_error', 'Invalid input'); |
|---|
| 77 |
$this->getParameterHolder()->set('pattern', null); |
|---|
| 78 |
|
|---|
| 79 |
$this->getParameterHolder()->add($parameters); |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
if ($this->getParameterHolder()->get('pattern') == null) |
|---|
| 83 |
{ |
|---|
| 84 |
|
|---|
| 85 |
throw new sfValidatorException('Please specify a PCRE regular expression pattern for your registered RegexValidator.'); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
return true; |
|---|
| 89 |
} |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|