| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TRangeValidator class file
|
|
|
4 |
*
|
|
|
5 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TRangeValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Web.UI.WebControls
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Using TBaseValidator class
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.Web.UI.WebControls.TBaseValidator');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* TRangeValidator class
|
|
|
20 |
*
|
|
|
21 |
* TRangeValidator tests whether an input value is within a specified range.
|
|
|
22 |
*
|
|
|
23 |
* TRangeValidator uses three key properties to perform its validation.
|
|
|
24 |
* The {@link setMinValue MinValue} and {@link setMaxValue MaxValue}
|
|
|
25 |
* properties specify the minimum and maximum values of the valid range.
|
|
|
26 |
* The {@link setDataType DataType} property is used to specify the
|
|
|
27 |
* data type of the value and the minimum and maximum range values.
|
|
|
28 |
* These values are converted to this data type before the validation
|
|
|
29 |
* operation is performed. The following value types are supported:
|
|
|
30 |
* - <b>Integer</b> A 32-bit signed integer data type.
|
|
|
31 |
* - <b>Float</b> A double-precision floating point number data type.
|
|
|
32 |
* - <b>Date</b> A date data type. The date format can be specified by
|
|
|
33 |
* setting {@link setDateFormat DateFormat} property, which must be recognizable
|
|
|
34 |
* by {@link TSimpleDateFormatter}. If the property is not set,
|
|
|
35 |
* the GNU date syntax is assumed.
|
|
|
36 |
* - <b>String</b> A string data type.
|
|
|
37 |
* - <b>StringLength</b> check for string length.
|
|
|
38 |
*
|
|
|
39 |
* If {@link setStrictComparison StrictComparison} is true, then the ranges
|
|
|
40 |
* are compared as strictly less than the max value and/or strictly greater than the min value.
|
|
|
41 |
*
|
|
|
42 |
* The TRangeValidator allows a special DataType "StringLength" that
|
|
|
43 |
* can be used to verify minimum and maximum string length. The
|
|
|
44 |
* {@link setCharset Charset} property can be used to force a particular
|
|
|
45 |
* charset for comparison. Otherwise, the application charset is used and is
|
|
|
46 |
* defaulted as UTF-8.
|
|
|
47 |
*
|
|
|
48 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
49 |
* @version $Id: TRangeValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
50 |
* @package System.Web.UI.WebControls
|
|
|
51 |
* @since 3.0
|
|
|
52 |
*/
|
|
|
53 |
class TRangeValidator extends TBaseValidator
|
|
|
54 |
{
|
|
|
55 |
/**
|
|
|
56 |
* Gets the name of the javascript class responsible for performing validation for this control.
|
|
|
57 |
* This method overrides the parent implementation.
|
|
|
58 |
* @return string the javascript class name
|
|
|
59 |
*/
|
|
|
60 |
protected function getClientClassName()
|
|
|
61 |
{
|
|
|
62 |
return 'Prado.WebUI.TRangeValidator';
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* @return string the minimum value of the validation range.
|
|
|
67 |
*/
|
|
|
68 |
public function getMinValue()
|
|
|
69 |
{
|
|
|
70 |
return $this->getViewState('MinValue','');
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Sets the minimum value of the validation range.
|
|
|
75 |
* @param string the minimum value
|
|
|
76 |
*/
|
|
|
77 |
public function setMinValue($value)
|
|
|
78 |
{
|
|
|
79 |
$this->setViewState('MinValue',TPropertyValue::ensureString($value),'');
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @return string the maximum value of the validation range.
|
|
|
84 |
*/
|
|
|
85 |
public function getMaxValue()
|
|
|
86 |
{
|
|
|
87 |
return $this->getViewState('MaxValue','');
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Sets the maximum value of the validation range.
|
|
|
92 |
* @param string the maximum value
|
|
|
93 |
*/
|
|
|
94 |
public function setMaxValue($value)
|
|
|
95 |
{
|
|
|
96 |
$this->setViewState('MaxValue',TPropertyValue::ensureString($value),'');
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* @param boolean true to perform strict comparison (i.e. strictly less than max and/or strictly greater than min).
|
|
|
101 |
*/
|
|
|
102 |
public function setStrictComparison($value)
|
|
|
103 |
{
|
|
|
104 |
$this->setViewState('StrictComparison', TPropertyValue::ensureBoolean($value),false);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* @return boolean true to perform strict comparison.
|
|
|
109 |
*/
|
|
|
110 |
public function getStrictComparison()
|
|
|
111 |
{
|
|
|
112 |
return $this->getViewState('StrictComparison', false);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* @return TRangeValidationDataType the data type that the values being compared are
|
|
|
117 |
* converted to before the comparison is made. Defaults to TRangeValidationDataType::String.
|
|
|
118 |
*/
|
|
|
119 |
public function getDataType()
|
|
|
120 |
{
|
|
|
121 |
return $this->getViewState('DataType',TRangeValidationDataType::String);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Sets the data type that the values being compared are converted to before the comparison is made.
|
|
|
126 |
* @param TRangeValidationDataType the data type
|
|
|
127 |
*/
|
|
|
128 |
public function setDataType($value)
|
|
|
129 |
{
|
|
|
130 |
$this->setViewState('DataType',TPropertyValue::ensureEnum($value,'TRangeValidationDataType'),TRangeValidationDataType::String);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Sets the date format for a date validation
|
|
|
135 |
* @param string the date format value
|
|
|
136 |
*/
|
|
|
137 |
public function setDateFormat($value)
|
|
|
138 |
{
|
|
|
139 |
$this->setViewState('DateFormat', $value, '');
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* @return string the date validation date format if any
|
|
|
144 |
*/
|
|
|
145 |
public function getDateFormat()
|
|
|
146 |
{
|
|
|
147 |
return $this->getViewState('DateFormat', '');
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* @param string charset for string length comparison.
|
|
|
152 |
*/
|
|
|
153 |
public function setCharset($value)
|
|
|
154 |
{
|
|
|
155 |
$this->setViewState('Charset', $value, '');
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* @return string charset for string length comparison.
|
|
|
160 |
*/
|
|
|
161 |
public function getCharset()
|
|
|
162 |
{
|
|
|
163 |
return $this->getViewState('Charset', '');
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* This method overrides the parent's implementation.
|
|
|
168 |
* The validation succeeds if the input data is within the range.
|
|
|
169 |
* The validation always succeeds if the input data is empty.
|
|
|
170 |
* @return boolean whether the validation succeeds
|
|
|
171 |
*/
|
|
|
172 |
protected function evaluateIsValid()
|
|
|
173 |
{
|
|
|
174 |
$value=$this->getValidationValue($this->getValidationTarget());
|
|
|
175 |
if($value==='')
|
|
|
176 |
return true;
|
|
|
177 |
|
|
|
178 |
switch($this->getDataType())
|
|
|
179 |
{
|
|
|
180 |
case TRangeValidationDataType::Integer:
|
|
|
181 |
return $this->isValidInteger($value);
|
|
|
182 |
case TRangeValidationDataType::Float:
|
|
|
183 |
return $this->isValidFloat($value);
|
|
|
184 |
case TRangeValidationDataType::Date:
|
|
|
185 |
return $this->isValidDate($value);
|
|
|
186 |
case TRangeValidationDataType::StringLength:
|
|
|
187 |
return $this->isValidStringLength($value);
|
|
|
188 |
default:
|
|
|
189 |
return $this->isValidString($value);
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Determine if the value is within the integer range.
|
|
|
195 |
* @param string value to validate true
|
|
|
196 |
* @return boolean true if within integer range.
|
|
|
197 |
*/
|
|
|
198 |
protected function isValidInteger($value)
|
|
|
199 |
{
|
|
|
200 |
$minValue=$this->getMinValue();
|
|
|
201 |
$maxValue=$this->getMaxValue();
|
|
|
202 |
|
|
|
203 |
$valid=preg_match('/^[-+]?[0-9]+$/',trim($value));
|
|
|
204 |
$value=intval($value);
|
|
|
205 |
if($minValue!=='')
|
|
|
206 |
$valid=$valid && $this->isGreaterThan($value, intval($minValue));
|
|
|
207 |
if($maxValue!=='')
|
|
|
208 |
$valid=$valid && $this->isLessThan($value,intval($maxValue));
|
|
|
209 |
return $valid;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
protected function isLessThan($left,$right)
|
|
|
213 |
{
|
|
|
214 |
return $this->getStrictComparison() ? $left < $right : $left <= $right;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
protected function isGreaterThan($left, $right)
|
|
|
218 |
{
|
|
|
219 |
return $this->getStrictComparison() ? $left > $right : $left >= $right;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Determine if the value is within the specified float range.
|
|
|
224 |
* @param string value to validate
|
|
|
225 |
* @return boolean true if within range.
|
|
|
226 |
*/
|
|
|
227 |
protected function isValidFloat($value)
|
|
|
228 |
{
|
|
|
229 |
$minValue=$this->getMinValue();
|
|
|
230 |
$maxValue=$this->getMaxValue();
|
|
|
231 |
|
|
|
232 |
$valid=preg_match('/^[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value));
|
|
|
233 |
$value=floatval($value);
|
|
|
234 |
if($minValue!=='')
|
|
|
235 |
$valid=$valid && $this->isGreaterThan($value,floatval($minValue));
|
|
|
236 |
if($maxValue!=='')
|
|
|
237 |
$valid=$valid && $this->isLessThan($value,floatval($maxValue));
|
|
|
238 |
return $valid;
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
/**
|
|
|
242 |
* Determine if the date is within the specified range.
|
|
|
243 |
* Uses pradoParseDate and strtotime to get the date from string.
|
|
|
244 |
* @param string date as string to validate
|
|
|
245 |
* @return boolean true if within range.
|
|
|
246 |
*/
|
|
|
247 |
protected function isValidDate($value)
|
|
|
248 |
{
|
|
|
249 |
$minValue=$this->getMinValue();
|
|
|
250 |
$maxValue=$this->getMaxValue();
|
|
|
251 |
|
|
|
252 |
$valid=true;
|
|
|
253 |
|
|
|
254 |
$dateFormat = $this->getDateFormat();
|
|
|
255 |
if($dateFormat!=='')
|
|
|
256 |
{
|
|
|
257 |
$formatter=Prado::createComponent('System.Util.TSimpleDateFormatter', $dateFormat);
|
|
|
258 |
$value = $formatter->parse($value, $dateFormat);
|
|
|
259 |
if($minValue!=='')
|
|
|
260 |
$valid=$valid && $this->isGreaterThan($value,$formatter->parse($minValue));
|
|
|
261 |
if($maxValue!=='')
|
|
|
262 |
$valid=$valid && $this->isLessThan($value,$formatter->parse($maxValue));
|
|
|
263 |
return $valid;
|
|
|
264 |
}
|
|
|
265 |
else
|
|
|
266 |
{
|
|
|
267 |
$value=strtotime($value);
|
|
|
268 |
if($minValue!=='')
|
|
|
269 |
$valid=$valid && $this->isGreaterThan($value,strtotime($minValue));
|
|
|
270 |
if($maxValue!=='')
|
|
|
271 |
$valid=$valid && $this->isLessThan($value,strtotime($maxValue));
|
|
|
272 |
return $valid;
|
|
|
273 |
}
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
/**
|
|
|
277 |
* Compare the string with a minimum and a maxiumum value.
|
|
|
278 |
* Uses strcmp for comparision.
|
|
|
279 |
* @param string value to compare with.
|
|
|
280 |
* @return boolean true if the string is within range.
|
|
|
281 |
*/
|
|
|
282 |
protected function isValidString($value)
|
|
|
283 |
{
|
|
|
284 |
$minValue=$this->getMinValue();
|
|
|
285 |
$maxValue=$this->getMaxValue();
|
|
|
286 |
|
|
|
287 |
$valid=true;
|
|
|
288 |
if($minValue!=='')
|
|
|
289 |
$valid=$valid && $this->isGreaterThan(strcmp($value,$minValue),0);
|
|
|
290 |
if($maxValue!=='')
|
|
|
291 |
$valid=$valid && $this->isLessThan(strcmp($value,$maxValue),0);
|
|
|
292 |
return $valid;
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* @param string string for comparision
|
|
|
297 |
* @return boolean true if min and max string length are satisfied.
|
|
|
298 |
*/
|
|
|
299 |
protected function isValidStringLength($value)
|
|
|
300 |
{
|
|
|
301 |
$minValue=$this->getMinValue();
|
|
|
302 |
$maxValue=$this->getMaxValue();
|
|
|
303 |
|
|
|
304 |
$valid=true;
|
|
|
305 |
$charset = $this->getCharset();
|
|
|
306 |
if($charset==='')
|
|
|
307 |
{
|
|
|
308 |
$app= $this->getApplication()->getGlobalization();
|
|
|
309 |
$charset = $app ? $app->getCharset() : null;
|
|
|
310 |
if(!$charset)
|
|
|
311 |
$charset = 'UTF-8';
|
|
|
312 |
}
|
|
|
313 |
|
|
|
314 |
$length = iconv_strlen($value, $charset);
|
|
|
315 |
if($minValue!=='')
|
|
|
316 |
$valid = $valid && $this->isGreaterThan($length,intval($minValue));
|
|
|
317 |
if($maxValue!=='')
|
|
|
318 |
$valid = $valid && $this->isLessThan($length,intval($maxValue));
|
|
|
319 |
return $valid;
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
/**
|
|
|
323 |
* Returns an array of javascript validator options.
|
|
|
324 |
* @return array javascript validator options.
|
|
|
325 |
*/
|
|
|
326 |
protected function getClientScriptOptions()
|
|
|
327 |
{
|
|
|
328 |
$options=parent::getClientScriptOptions();
|
|
|
329 |
$options['MinValue']=$this->getMinValue();
|
|
|
330 |
$options['MaxValue']=$this->getMaxValue();
|
|
|
331 |
$options['DataType']=$this->getDataType();
|
|
|
332 |
$options['StrictComparison']=$this->getStrictComparison();
|
|
|
333 |
if(($dateFormat=$this->getDateFormat())!=='')
|
|
|
334 |
$options['DateFormat']=$dateFormat;
|
|
|
335 |
return $options;
|
|
|
336 |
}
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
/**
|
|
|
341 |
* TRangeValidationDataType class.
|
|
|
342 |
* TRangeValidationDataType defines the enumerable type for the possible data types that
|
|
|
343 |
* a range validator can validate upon.
|
|
|
344 |
*
|
|
|
345 |
* The following enumerable values are defined:
|
|
|
346 |
* - Integer
|
|
|
347 |
* - Float
|
|
|
348 |
* - Date
|
|
|
349 |
* - String
|
|
|
350 |
* - StringLength
|
|
|
351 |
*
|
|
|
352 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
353 |
* @version $Id: TRangeValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
354 |
* @package System.Web.UI.WebControls
|
|
|
355 |
* @since 3.0.4
|
|
|
356 |
*/
|
|
|
357 |
class TRangeValidationDataType extends TValidationDataType
|
|
|
358 |
{
|
|
|
359 |
const StringLength='StringLength';
|
|
|
360 |
}
|