sfValidatorRegex.class.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfValidatorRegex validates a value with a regular expression.
  11. *
  12. * @package symfony
  13. * @subpackage validator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfValidatorRegex.class.php 12081 2008-10-08 19:01:19Z fabien $
  16. */
  17. class sfValidatorRegex extends sfValidatorString
  18. {
  19. /**
  20. * Configures the current validator.
  21. *
  22. * Available options:
  23. *
  24. * * pattern: A regex pattern compatible with PCRE (required)
  25. *
  26. * @param array $options An array of options
  27. * @param array $messages An array of error messages
  28. *
  29. * @see sfValidatorString
  30. */
  31. protected function configure($options = array(), $messages = array())
  32. {
  33. parent::configure($options, $messages);
  34. $this->addRequiredOption('pattern');
  35. }
  36. /**
  37. * @see sfValidatorString
  38. */
  39. protected function doClean($value)
  40. {
  41. $clean = parent::doClean($value);
  42. if (!preg_match($this->getOption('pattern'), $clean))
  43. {
  44. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  45. }
  46. return $clean;
  47. }
  48. }