| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TCompareValidator 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: TCompareValidator.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 |
* TCompareValidator class
|
|
|
20 |
*
|
|
|
21 |
* TCompareValidator compares the value entered by the user into an input
|
|
|
22 |
* control with the value entered into another input control or a constant value.
|
|
|
23 |
* To compare the associated input control with another input control,
|
|
|
24 |
* set the {@link setControlToCompare ControlToCompare} property to the ID path
|
|
|
25 |
* of the control to compare with. To compare the associated input control with
|
|
|
26 |
* a constant value, specify the constant value to compare with by setting the
|
|
|
27 |
* {@link setValueToCompare ValueToCompare} property.
|
|
|
28 |
*
|
|
|
29 |
* The {@link setDataType DataType} property is used to specify the data type
|
|
|
30 |
* of both comparison values. Both values are automatically converted to this data
|
|
|
31 |
* type before the comparison operation is performed. The following value types are supported:
|
|
|
32 |
* - <b>Integer</b> A 32-bit signed integer data type.
|
|
|
33 |
* - <b>Float</b> A double-precision floating point number data type.
|
|
|
34 |
* - <b>Date</b> A date data type. The format can be specified by the
|
|
|
35 |
* {@link setDateFormat DateFormat} property
|
|
|
36 |
* - <b>String</b> A string data type.
|
|
|
37 |
*
|
|
|
38 |
* Use the {@link setOperator Operator} property to specify the type of comparison
|
|
|
39 |
* to perform. Valid operators include Equal, NotEqual, GreaterThan, GreaterThanEqual,
|
|
|
40 |
* LessThan and LessThanEqual.
|
|
|
41 |
*
|
|
|
42 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
43 |
* @version $Id: TCompareValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
44 |
* @package System.Web.UI.WebControls
|
|
|
45 |
* @since 3.0
|
|
|
46 |
*/
|
|
|
47 |
class TCompareValidator extends TBaseValidator
|
|
|
48 |
{
|
|
|
49 |
/**
|
|
|
50 |
* Gets the name of the javascript class responsible for performing validation for this control.
|
|
|
51 |
* This method overrides the parent implementation.
|
|
|
52 |
* @return string the javascript class name
|
|
|
53 |
*/
|
|
|
54 |
protected function getClientClassName()
|
|
|
55 |
{
|
|
|
56 |
return 'Prado.WebUI.TCompareValidator';
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* @return TValidationDataType the data type that the values being compared are converted to before the comparison is made. Defaults to TValidationDataType::String.
|
|
|
61 |
*/
|
|
|
62 |
public function getDataType()
|
|
|
63 |
{
|
|
|
64 |
return $this->getViewState('DataType',TValidationDataType::String);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Sets the data type that the values being
|
|
|
69 |
* compared are converted to before the comparison is made.
|
|
|
70 |
* @param TValidationDataType the data type
|
|
|
71 |
*/
|
|
|
72 |
public function setDataType($value)
|
|
|
73 |
{
|
|
|
74 |
$this->setViewState('DataType',TPropertyValue::ensureEnum($value,'TValidationDataType'),TValidationDataType::String);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* @return string the input component to compare with the input control being validated.
|
|
|
79 |
*/
|
|
|
80 |
public function getControlToCompare()
|
|
|
81 |
{
|
|
|
82 |
return $this->getViewState('ControlToCompare','');
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Sets the input component to compare with the input control being validated.
|
|
|
87 |
* @param string the ID path of the component to compare with
|
|
|
88 |
*/
|
|
|
89 |
public function setControlToCompare($value)
|
|
|
90 |
{
|
|
|
91 |
$this->setViewState('ControlToCompare',$value,'');
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* @return string the constant value to compare with the value entered by the user into the input component being validated.
|
|
|
96 |
*/
|
|
|
97 |
public function getValueToCompare()
|
|
|
98 |
{
|
|
|
99 |
return $this->getViewState('ValueToCompare','');
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Sets the constant value to compare with the value entered by the user into the input component being validated.
|
|
|
104 |
* @param string the constant value
|
|
|
105 |
*/
|
|
|
106 |
public function setValueToCompare($value)
|
|
|
107 |
{
|
|
|
108 |
$this->setViewState('ValueToCompare',$value,'');
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* @return TValidationCompareOperator the comparison operation to perform. Defaults to TValidationCompareOperator::Equal.
|
|
|
113 |
*/
|
|
|
114 |
public function getOperator()
|
|
|
115 |
{
|
|
|
116 |
return $this->getViewState('Operator',TValidationCompareOperator::Equal);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Sets the comparison operation to perform
|
|
|
121 |
* @param TValidationCompareOperator the comparison operation
|
|
|
122 |
*/
|
|
|
123 |
public function setOperator($value)
|
|
|
124 |
{
|
|
|
125 |
$this->setViewState('Operator',TPropertyValue::ensureEnum($value,'TValidationCompareOperator'),TValidationCompareOperator::Equal);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Sets the date format for a date validation
|
|
|
130 |
* @param string the date format value
|
|
|
131 |
*/
|
|
|
132 |
public function setDateFormat($value)
|
|
|
133 |
{
|
|
|
134 |
$this->setViewState('DateFormat', $value, '');
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* @return string the date validation date format if any
|
|
|
139 |
*/
|
|
|
140 |
public function getDateFormat()
|
|
|
141 |
{
|
|
|
142 |
return $this->getViewState('DateFormat', '');
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* This method overrides the parent's implementation.
|
|
|
147 |
* The validation succeeds if the input data compares successfully.
|
|
|
148 |
* The validation always succeeds if ControlToValidate is not specified
|
|
|
149 |
* or the input data is empty.
|
|
|
150 |
* @return boolean whether the validation succeeds
|
|
|
151 |
*/
|
|
|
152 |
public function evaluateIsValid()
|
|
|
153 |
{
|
|
|
154 |
if(($value=$this->getValidationValue($this->getValidationTarget()))==='')
|
|
|
155 |
return true;
|
|
|
156 |
|
|
|
157 |
if(($controlToCompare=$this->getControlToCompare())!=='')
|
|
|
158 |
{
|
|
|
159 |
if(($control2=$this->findControl($controlToCompare))===null)
|
|
|
160 |
throw new TInvalidDataValueException('comparevalidator_controltocompare_invalid');
|
|
|
161 |
if(($value2=$this->getValidationValue($control2))==='')
|
|
|
162 |
return false;
|
|
|
163 |
}
|
|
|
164 |
else
|
|
|
165 |
$value2=$this->getValueToCompare();
|
|
|
166 |
|
|
|
167 |
$values = $this->getComparisonValues($value, $value2);
|
|
|
168 |
switch($this->getOperator())
|
|
|
169 |
{
|
|
|
170 |
case TValidationCompareOperator::Equal:
|
|
|
171 |
return $values[0] == $values[1];
|
|
|
172 |
case TValidationCompareOperator::NotEqual:
|
|
|
173 |
return $values[0] != $values[1];
|
|
|
174 |
case TValidationCompareOperator::GreaterThan:
|
|
|
175 |
return $values[0] > $values[1];
|
|
|
176 |
case TValidationCompareOperator::GreaterThanEqual:
|
|
|
177 |
return $values[0] >= $values[1];
|
|
|
178 |
case TValidationCompareOperator::LessThan:
|
|
|
179 |
return $values[0] < $values[1];
|
|
|
180 |
case TValidationCompareOperator::LessThanEqual:
|
|
|
181 |
return $values[0] <= $values[1];
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
return false;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Parse the pair of values into the appropriate value type.
|
|
|
189 |
* @param string value one
|
|
|
190 |
* @param string second value
|
|
|
191 |
* @return array appropriate type of the value pair, array($value1, $value2);
|
|
|
192 |
*/
|
|
|
193 |
protected function getComparisonValues($value1, $value2)
|
|
|
194 |
{
|
|
|
195 |
switch($this->getDataType())
|
|
|
196 |
{
|
|
|
197 |
case TValidationDataType::Integer:
|
|
|
198 |
return array(intval($value1), intval($value2));
|
|
|
199 |
case TValidationDataType::Float:
|
|
|
200 |
return array(floatval($value1), floatval($value2));
|
|
|
201 |
case TValidationDataType::Date:
|
|
|
202 |
$dateFormat = $this->getDateFormat();
|
|
|
203 |
if($dateFormat!=='')
|
|
|
204 |
{
|
|
|
205 |
$formatter = Prado::createComponent('System.Util.TSimpleDateFormatter', $dateFormat);
|
|
|
206 |
return array($formatter->parse($value1), $formatter->parse($value2));
|
|
|
207 |
}
|
|
|
208 |
else
|
|
|
209 |
return array(strtotime($value1), strtotime($value2));
|
|
|
210 |
}
|
|
|
211 |
return array($value1, $value2);
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
/**
|
|
|
215 |
* Returns an array of javascript validator options.
|
|
|
216 |
* @return array javascript validator options.
|
|
|
217 |
*/
|
|
|
218 |
protected function getClientScriptOptions()
|
|
|
219 |
{
|
|
|
220 |
$options = parent::getClientScriptOptions();
|
|
|
221 |
if(($name=$this->getControlToCompare())!=='')
|
|
|
222 |
{
|
|
|
223 |
if(($control=$this->findControl($name))!==null)
|
|
|
224 |
$options['ControlToCompare']=$control->getClientID();
|
|
|
225 |
}
|
|
|
226 |
if(($value=$this->getValueToCompare())!=='')
|
|
|
227 |
$options['ValueToCompare']=$value;
|
|
|
228 |
if(($operator=$this->getOperator())!==TValidationCompareOperator::Equal)
|
|
|
229 |
$options['Operator']=$operator;
|
|
|
230 |
$options['DataType']=$this->getDataType();
|
|
|
231 |
if(($dateFormat=$this->getDateFormat())!=='')
|
|
|
232 |
$options['DateFormat']=$dateFormat;
|
|
|
233 |
return $options;
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
|
|
|
238 |
/**
|
|
|
239 |
* TValidationCompareOperator class.
|
|
|
240 |
* TValidationCompareOperator defines the enumerable type for the comparison operations
|
|
|
241 |
* that {@link TCompareValidator} can perform validation with.
|
|
|
242 |
*
|
|
|
243 |
* The following enumerable values are defined:
|
|
|
244 |
* - Equal
|
|
|
245 |
* - NotEqual
|
|
|
246 |
* - GreaterThan
|
|
|
247 |
* - GreaterThanEqual
|
|
|
248 |
* - LessThan
|
|
|
249 |
* - LessThanEqual
|
|
|
250 |
*
|
|
|
251 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
252 |
* @version $Id: TCompareValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
253 |
* @package System.Web.UI.WebControls
|
|
|
254 |
* @since 3.0.4
|
|
|
255 |
*/
|
|
|
256 |
class TValidationCompareOperator extends TEnumerable
|
|
|
257 |
{
|
|
|
258 |
const Equal='Equal';
|
|
|
259 |
const NotEqual='NotEqual';
|
|
|
260 |
const GreaterThan='GreaterThan';
|
|
|
261 |
const GreaterThanEqual='GreaterThanEqual';
|
|
|
262 |
const LessThan='LessThan';
|
|
|
263 |
const LessThanEqual='LessThanEqual';
|
|
|
264 |
}
|
|
|
265 |
|