sfValidatorRegex.class.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 9048 2008-05-19 09:11:23Z FabianLange $
  16. */
  17. class sfValidatorRegex extends sfValidatorBase
  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 sfValidatorBase
  30. */
  31. protected function configure($options = array(), $messages = array())
  32. {
  33. $this->addRequiredOption('pattern');
  34. }
  35. /**
  36. * @see sfValidatorBase
  37. */
  38. protected function doClean($value)
  39. {
  40. $clean = (string) $value;
  41. if (!preg_match($this->getOption('pattern'), $clean))
  42. {
  43. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  44. }
  45. return $clean;
  46. }
  47. }