1. sfValidatorString.class.php
  2. /** * sfValidatorString validates a string. It also converts the input value to a string. * * @package symfony * @subpackage validator * @author Fabien Potencier * @version SVN: $Id: sfValidatorString.class.php 12641 2008-11-04 18:22:00Z fabien $ */
  3. class sfValidatorString extends sfValidatorBase
  4. {
  5. /**
  6. * Configures the current validator.
  7. *
  8. * Available options:
  9. *
  10. * * max_length: The maximum length of the string
  11. * * min_length: The minimum length of the string
  12. *
  13. * Available error codes:
  14. *
  15. * * max_length
  16. * * min_length
  17. *
  18. * @param array $options An array of options
  19. * @param array $messages An array of error messages
  20. *
  21. * @see sfValidatorBase
  22. */
  23. protected function configure($options = array(), $messages = array())
  24. {
  25. $this->addMessage('max_length', '"%value%" is too long (%max_length% characters max).');
  26. $this->addMessage('min_length', '"%value%" is too short (%min_length% characters min).');
  27. $this->addOption('max_length');
  28. $this->addOption('min_length');
  29. $this->setOption('empty_value', '');
  30. }
  31. /**
  32. * @see sfValidatorBase
  33. */
  34. protected function doClean($value)
  35. {
  36. $clean = (string) $value;
  37. $length = function_exists('mb_strlen') ? mb_strlen($clean, $this->getCharset()) : strlen($clean);
  38. if ($this->hasOption('max_length') && $length > $this->getOption('max_length'))
  39. {
  40. throw new sfValidatorError($this, 'max_length', array('value' => $value, 'max_length' => $this->getOption('max_length')));
  41. }
  42. if ($this->hasOption('min_length') && $length < $this->getOption('min_length'))
  43. {
  44. throw new sfValidatorError($this, 'min_length', array('value' => $value, 'min_length' => $this->getOption('min_length')));
  45. }
  46. return $clean;
  47. }
  48. }

Debug toolbar