1. sfValidatorRegex.class.php
  2. /** * sfValidatorRegex validates a value with a regular expression. * * @package symfony * @subpackage validator * @author Fabien Potencier * @version SVN: $Id: sfValidatorRegex.class.php 22149 2009-09-18 14:09:53Z Kris.Wallsmith $ */
  3. class sfValidatorRegex extends sfValidatorString
  4. {
  5. /**
  6. * Configures the current validator.
  7. *
  8. * Available options:
  9. *
  10. * * pattern: A regex pattern compatible with PCRE or {@link sfCallable} that returns one (required)
  11. * * must_match: Whether the regex must match or not (true by default)
  12. *
  13. * @param array $options An array of options
  14. * @param array $messages An array of error messages
  15. *
  16. * @see sfValidatorString
  17. */
  18. protected function configure($options = array(), $messages = array())
  19. {
  20. parent::configure($options, $messages);
  21. $this->addRequiredOption('pattern');
  22. $this->addOption('must_match', true);
  23. }
  24. /**
  25. * @see sfValidatorString
  26. */
  27. protected function doClean($value)
  28. {
  29. $clean = parent::doClean($value);
  30. $pattern = $this->getPattern();
  31. if (
  32. ($this->getOption('must_match') && !preg_match($pattern, $clean))
  33. ||
  34. (!$this->getOption('must_match') && preg_match($pattern, $clean))
  35. )
  36. {
  37. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  38. }
  39. return $clean;
  40. }
  41. /**
  42. * Returns the current validator's regular expression.
  43. *
  44. * @return string
  45. */
  46. public function getPattern()
  47. {
  48. $pattern = $this->getOption('pattern');
  49. return $pattern instanceof sfCallable ? $pattern->call() : $pattern;
  50. }
  51. }

Debug toolbar