123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- class sfValidatorNumber extends sfValidatorBase
- {
-
- protected function configure($options = array(), $messages = array())
- {
- $this->addMessage('max', '"%value%" must be less than %max%.');
- $this->addMessage('min', '"%value%" must be greater than %min%.');
- $this->addOption('min');
- $this->addOption('max');
- $this->setMessage('invalid', '"%value%" is not a number.');
- }
-
- protected function doClean($value)
- {
- if (!is_numeric($value))
- {
- throw new sfValidatorError($this, 'invalid', array('value' => $value));
- }
- $clean = floatval($value);
- if ($this->hasOption('max') && $clean > $this->getOption('max'))
- {
- throw new sfValidatorError($this, 'max', array('value' => $value, 'max' => $this->getOption('max')));
- }
- if ($this->hasOption('min') && $clean < $this->getOption('min'))
- {
- throw new sfValidatorError($this, 'min', array('value' => $value, 'min' => $this->getOption('min')));
- }
- return $clean;
- }
- }
|