Development

/branches/1.4/lib/validator/sfValidatorNumber.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/validator/sfValidatorNumber.class.php

Revision 22018, 1.9 kB (checked in by fabien, 5 years ago)

[1.2, 1.3] fixed number validators default min/max error messages (closes #7035, #6485)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfValidatorNumber validates a number (integer or float). It also converts the input value to a float.
13  *
14  * @package    symfony
15  * @subpackage validator
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfValidatorNumber extends sfValidatorBase
20 {
21   /**
22    * Configures the current validator.
23    *
24    * Available options:
25    *
26    *  * max: The maximum value allowed
27    *  * min: The minimum value allowed
28    *
29    * Available error codes:
30    *
31    *  * max
32    *  * min
33    *
34    * @param array $options   An array of options
35    * @param array $messages  An array of error messages
36    *
37    * @see sfValidatorBase
38    */
39   protected function configure($options = array(), $messages = array())
40   {
41     $this->addMessage('max', '"%value%" must be at most %max%.');
42     $this->addMessage('min', '"%value%" must be at least %min%.');
43
44     $this->addOption('min');
45     $this->addOption('max');
46
47     $this->setMessage('invalid', '"%value%" is not a number.');
48   }
49
50   /**
51    * @see sfValidatorBase
52    */
53   protected function doClean($value)
54   {
55     if (!is_numeric($value))
56     {
57       throw new sfValidatorError($this, 'invalid', array('value' => $value));
58     }
59
60     $clean = floatval($value);
61
62     if ($this->hasOption('max') && $clean > $this->getOption('max'))
63     {
64       throw new sfValidatorError($this, 'max', array('value' => $value, 'max' => $this->getOption('max')));
65     }
66
67     if ($this->hasOption('min') && $clean < $this->getOption('min'))
68     {
69       throw new sfValidatorError($this, 'min', array('value' => $value, 'min' => $this->getOption('min')));
70     }
71
72     return $clean;
73   }
74 }
75
Note: See TracBrowser for help on using the browser.