|
Revision 22149, 1.8 kB
(checked in by Kris.Wallsmith, 4 years ago)
|
[1.3] added a check for sfCallable in regex validator, added protocols option to url validator, moved email pattern to class constant
|
- Property svn:mime-type set to
text/x-php
- Property svn:eol-style set to
native
- Property svn:keywords set to
Id
|
| Line | |
|---|
| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfValidatorRegex extends sfValidatorString |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
* Configures the current validator. |
|---|
| 23 |
* |
|---|
| 24 |
* Available options: |
|---|
| 25 |
* |
|---|
| 26 |
* * pattern: A regex pattern compatible with PCRE or {@link sfCallable} that returns one (required) |
|---|
| 27 |
* * must_match: Whether the regex must match or not (true by default) |
|---|
| 28 |
* |
|---|
| 29 |
* @param array $options An array of options |
|---|
| 30 |
* @param array $messages An array of error messages |
|---|
| 31 |
* |
|---|
| 32 |
* @see sfValidatorString |
|---|
| 33 |
*/ |
|---|
| 34 |
protected function configure($options = array(), $messages = array()) |
|---|
| 35 |
{ |
|---|
| 36 |
parent::configure($options, $messages); |
|---|
| 37 |
|
|---|
| 38 |
$this->addRequiredOption('pattern'); |
|---|
| 39 |
$this->addOption('must_match', true); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
* @see sfValidatorString |
|---|
| 44 |
*/ |
|---|
| 45 |
protected function doClean($value) |
|---|
| 46 |
{ |
|---|
| 47 |
$clean = parent::doClean($value); |
|---|
| 48 |
|
|---|
| 49 |
$pattern = $this->getPattern(); |
|---|
| 50 |
|
|---|
| 51 |
if ( |
|---|
| 52 |
($this->getOption('must_match') && !preg_match($pattern, $clean)) |
|---|
| 53 |
|| |
|---|
| 54 |
(!$this->getOption('must_match') && preg_match($pattern, $clean)) |
|---|
| 55 |
) |
|---|
| 56 |
{ |
|---|
| 57 |
throw new sfValidatorError($this, 'invalid', array('value' => $value)); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
return $clean; |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
* Returns the current validator's regular expression. |
|---|
| 65 |
* |
|---|
| 66 |
* @return string |
|---|
| 67 |
*/ |
|---|
| 68 |
public function getPattern() |
|---|
| 69 |
{ |
|---|
| 70 |
$pattern = $this->getOption('pattern'); |
|---|
| 71 |
|
|---|
| 72 |
return $pattern instanceof sfCallable ? $pattern->call() : $pattern; |
|---|
| 73 |
} |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|