1. sfValidatorNumber.class.php
  2. /** * sfValidatorNumber validates a number (integer or float). It also converts the input value to a float. * * @package symfony * @subpackage validator * @author Fabien Potencier * @version SVN: $Id: sfValidatorNumber.class.php 22018 2009-09-14 16:56:28Z fabien $ */
  3. class sfValidatorNumber extends sfValidatorBase
  4. {
  5. /**
  6. * Configures the current validator.
  7. *
  8. * Available options:
  9. *
  10. * * max: The maximum value allowed
  11. * * min: The minimum value allowed
  12. *
  13. * Available error codes:
  14. *
  15. * * max
  16. * * min
  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', '"%value%" must be at most %max%.');
  26. $this->addMessage('min', '"%value%" must be at least %min%.');
  27. $this->addOption('min');
  28. $this->addOption('max');
  29. $this->setMessage('invalid', '"%value%" is not a number.');
  30. }
  31. /**
  32. * @see sfValidatorBase
  33. */
  34. protected function doClean($value)
  35. {
  36. if (!is_numeric($value))
  37. {
  38. throw new sfValidatorError($this, 'invalid', array('value' => $value));
  39. }
  40. $clean = floatval($value);
  41. if ($this->hasOption('max') && $clean > $this->getOption('max'))
  42. {
  43. throw new sfValidatorError($this, 'max', array('value' => $value, 'max' => $this->getOption('max')));
  44. }
  45. if ($this->hasOption('min') && $clean < $this->getOption('min'))
  46. {
  47. throw new sfValidatorError($this, 'min', array('value' => $value, 'min' => $this->getOption('min')));
  48. }
  49. return $clean;
  50. }
  51. }

Debug toolbar