Development

/branches/1.2/lib/plugins/sfCompat10Plugin/lib/validator/sfNumberValidator.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfCompat10Plugin/lib/validator/sfNumberValidator.class.php

Revision 8053, 4.6 kB (checked in by Carl.Vondrick, 7 years ago)

1.1: fixed sfNumberValidator doesn't like numbers less than 0 (closes #3185)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfNumberValidator verifies a parameter is a number and allows you to apply
14  * size constraints.
15  *
16  * <b>Optional parameters:</b>
17  *
18  * # <b>max</b>        - [none]                  - Maximum number size.
19  * # <b>max_error</b>  - [Input is too large]    - An error message to use when
20  *                                                 input is too large.
21  * # <b>min</b>        - [none]                  - Minimum number size.
22  * # <b>min_error</b>  - [Input is too small]    - An error message to use when
23  *                                                 input is too small.
24  * # <b>nan_error</b>  - [Input is not a number] - Default error message when
25  *                                                 input is not a number.
26  * # <b>type</b>       - [Any]                   - Type of number (Any, Float).
27  * # <b>type_error</b> - [Input is not a number] - An error message to use when
28  *                                                 input is not a number.
29  *
30  * @package    symfony
31  * @subpackage validator
32  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
33  * @author     Sean Kerr <sean@code-box.org>
34  * @version    SVN: $Id$
35  */
36 class sfNumberValidator extends sfValidator
37 {
38   /**
39    * Executes this validator.
40    *
41    * @param mixed A file or parameter value/array
42    * @param error An error message reference
43    *
44    * @return bool true, if this validator executes successfully, otherwise false
45    */
46   public function execute(&$value, &$error)
47   {
48     if (!preg_match('/^-?\d+(\.\d+)?$/', $value))
49     {
50       // it's NaN, what nerve!
51       $error = $this->getParameterHolder()->get('nan_error');
52
53       return false;
54     }
55
56     $type = strtolower($this->getParameterHolder()->get('type'));
57
58     switch ($type)
59     {
60       case "decimal":
61       case "float":
62       {
63         if (substr_count($value, '.') != 1)
64         {
65           // value isn't a float, shazbot!
66           $error = $this->getParameterHolder()->get('type_error');
67           return false;
68         }
69
70         // cast our value to a float
71         $value = (float) $value;
72
73         break;
74       }
75
76       case "int":
77       case "integer":
78       {
79         // Note: (Both 3 AND 3.0 are BOTH considered integers and 3.1 is not)
80         if ((float) $value != (int) $value)
81         {
82           // is not an integer.
83           $error = $this->getParameterHolder()->get('type_error');
84           return false;
85         }
86
87         // cast our value to an integer
88         $value = (int) $value;
89
90         break;
91       }
92
93     }
94
95     $min = $this->getParameterHolder()->get('min');
96
97     if ($min !== null && $value < $min)
98     {
99       // too small
100       $error = $this->getParameterHolder()->get('min_error');
101
102       return false;
103     }
104
105     $max = $this->getParameterHolder()->get('max');
106
107     if ($max !== null && $value > $max)
108     {
109       // too large
110       $error = $this->getParameterHolder()->get('max_error');
111
112       return false;
113     }
114
115     return true;
116   }
117
118   /**
119    * Initializes this validator.
120    *
121    * @param sfContext The current application context
122    * @param array   An associative array of initialization parameters
123    *
124    * @return bool true, if initialization completes successfully, otherwise false
125    */
126   public function initialize($context, $parameters = null)
127   {
128     // initialize parent
129     parent::initialize($context);
130
131     // set defaults
132     $this->getParameterHolder()->set('max',        null);
133     $this->getParameterHolder()->set('max_error''Input is too large');
134     $this->getParameterHolder()->set('min',        null);
135     $this->getParameterHolder()->set('min_error''Input is too small');
136     $this->getParameterHolder()->set('nan_error''Input is not a number');
137     $this->getParameterHolder()->set('type',       'any');
138     $this->getParameterHolder()->set('type_error', 'Input is not a number');
139
140     $this->getParameterHolder()->add($parameters);
141
142     // check user-specified parameters
143     $type = strtolower($this->getParameterHolder()->get('type'));
144
145     // array of allowed types
146     $allowed_types = array('any', 'decimal', 'float', 'int', 'integer');
147
148     if (!in_array(strtolower($type), $allowed_types))
149     {
150       // unknown type
151       throw new sfValidatorException(sprintf('Unknown number type "%s" in NumberValidator.', $this->getParameterHolder()->get('type')));
152     }
153
154     return true;
155   }
156 }
157
Note: See TracBrowser for help on using the browser.