| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TBaseValidator 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: TBaseValidator.php 2551 2008-10-30 17:07:05Z carlgmathisen $
|
|
|
10 |
* @package System.Web.UI.WebControls
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Using TLabel class
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.Web.UI.WebControls.TLabel');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* TBaseValidator class
|
|
|
20 |
*
|
|
|
21 |
* TBaseValidator serves as the base class for validator controls.
|
|
|
22 |
*
|
|
|
23 |
* Validation is performed when a postback control, such as a TButton, a TLinkButton
|
|
|
24 |
* or a TTextBox (under AutoPostBack mode) is submitting the page and
|
|
|
25 |
* its <b>CausesValidation</b> property is true.
|
|
|
26 |
* You can also manually perform validation by calling {@link TPage::validate()}.
|
|
|
27 |
* The input control to be validated is specified by {@link setControlToValidate ControlToValidate}.
|
|
|
28 |
*
|
|
|
29 |
* Validator controls always validate the associated input control on the serve side.
|
|
|
30 |
* In addition, if {@link getEnableClientScript EnableClientScript} is true,
|
|
|
31 |
* validation will also be performed on the client-side using javascript.
|
|
|
32 |
* Client-side validation will validate user input before it is sent to the server.
|
|
|
33 |
* The form data will not be submitted if any error is detected. This avoids
|
|
|
34 |
* the round-trip of information necessary for server-side validation.
|
|
|
35 |
*
|
|
|
36 |
* You can use multiple validator controls to validate a single input control,
|
|
|
37 |
* each responsible for validating against a different criteria.
|
|
|
38 |
* For example, on a user registration form, you may want to make sure the user
|
|
|
39 |
* enters a value in the username text box, and the input must consist of only word
|
|
|
40 |
* characters. You can use a {@link TRequiredFieldValidator} to ensure the input
|
|
|
41 |
* of username and a {@link TRegularExpressionValidator} to ensure the proper input.
|
|
|
42 |
*
|
|
|
43 |
* If an input control fails validation, the text specified by the {@link setErrorMessage ErrorMessage}
|
|
|
44 |
* property is displayed in the validation control. However, if the {@link setText Text}
|
|
|
45 |
* property is set, it will be displayed instead. If both {@link setErrorMessage ErrorMessage}
|
|
|
46 |
* and {@link setText Text} are empty, the body content of the validator will
|
|
|
47 |
* be displayed. Error display is controlled by {@link setDisplay Display} property.
|
|
|
48 |
*
|
|
|
49 |
* You can also customized the client-side behaviour by adding javascript
|
|
|
50 |
* code to the subproperties of the {@link getClientSide ClientSide}
|
|
|
51 |
* property. See quickstart documentation for further details.
|
|
|
52 |
*
|
|
|
53 |
* You can also place a {@link TValidationSummary} control on a page to display error messages
|
|
|
54 |
* from the validators together. In this case, only the {@link setErrorMessage ErrorMessage}
|
|
|
55 |
* property of the validators will be displayed in the {@link TValidationSummary} control.
|
|
|
56 |
*
|
|
|
57 |
* Validators can be partitioned into validation groups by setting their
|
|
|
58 |
* {@link setValidationGroup ValidationGroup} property. If the control causing the
|
|
|
59 |
* validation also sets its ValidationGroup property, only those validators having
|
|
|
60 |
* the same ValidationGroup value will do input validation.
|
|
|
61 |
*
|
|
|
62 |
* Note, the {@link TPage::getIsValid IsValid} property of the current {@link TPage}
|
|
|
63 |
* instance will be automatically updated by the validation process which occurs
|
|
|
64 |
* after {@link TPage::onLoad onLoad} of {@link TPage} and before the postback events.
|
|
|
65 |
* Therefore, if you use the {@link TPage::getIsValid()} property in
|
|
|
66 |
* the {@link TPage::onLoad()} method, you must first explicitly call
|
|
|
67 |
* the {@link TPage::validate()} method.
|
|
|
68 |
*
|
|
|
69 |
* <b>Notes to Inheritors</b> When you inherit from TBaseValidator, you must
|
|
|
70 |
* override the method {@link evaluateIsValid}.
|
|
|
71 |
*
|
|
|
72 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
73 |
* @version $Id: TBaseValidator.php 2551 2008-10-30 17:07:05Z carlgmathisen $
|
|
|
74 |
* @package System.Web.UI.WebControls
|
|
|
75 |
* @since 3.0
|
|
|
76 |
*/
|
|
|
77 |
abstract class TBaseValidator extends TLabel implements IValidator
|
|
|
78 |
{
|
|
|
79 |
/**
|
|
|
80 |
* @var boolean whether the validation succeeds
|
|
|
81 |
*/
|
|
|
82 |
private $_isValid=true;
|
|
|
83 |
/**
|
|
|
84 |
* @var boolean whether the validator has been registered with the page
|
|
|
85 |
*/
|
|
|
86 |
private $_registered=false;
|
|
|
87 |
/**
|
|
|
88 |
* @var TValidatorClientSide validator client-script options.
|
|
|
89 |
*/
|
|
|
90 |
private $_clientSide;
|
|
|
91 |
/**
|
|
|
92 |
* Controls for which the client-side validation3.js file needs to handle
|
|
|
93 |
* them specially.
|
|
|
94 |
* @var array list of control class names
|
|
|
95 |
*/
|
|
|
96 |
private static $_clientClass = array('THtmlArea', 'TDatePicker', 'TListBox', 'TCheckBoxList');
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Constructor.
|
|
|
100 |
* This method sets the foreground color to red.
|
|
|
101 |
*/
|
|
|
102 |
public function __construct()
|
|
|
103 |
{
|
|
|
104 |
parent::__construct();
|
|
|
105 |
$this->setForeColor('red');
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Registers the validator with page.
|
|
|
110 |
* @param mixed event parameter
|
|
|
111 |
*/
|
|
|
112 |
public function onInit($param)
|
|
|
113 |
{
|
|
|
114 |
parent::onInit($param);
|
|
|
115 |
$this->getPage()->getValidators()->add($this);
|
|
|
116 |
$this->_registered=true;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Unregisters the validator from page.
|
|
|
121 |
* @param mixed event parameter
|
|
|
122 |
*/
|
|
|
123 |
public function onUnload($param)
|
|
|
124 |
{
|
|
|
125 |
if($this->_registered && ($page=$this->getPage())!==null)
|
|
|
126 |
$page->getValidators()->remove($this);
|
|
|
127 |
$this->_registered=false;
|
|
|
128 |
parent::onUnload($param);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Adds attributes to renderer. Calls parent implementation and renders the
|
|
|
133 |
* client control scripts.
|
|
|
134 |
* @param THtmlWriter the renderer
|
|
|
135 |
*/
|
|
|
136 |
protected function addAttributesToRender($writer)
|
|
|
137 |
{
|
|
|
138 |
$display=$this->getDisplay();
|
|
|
139 |
$visible=$this->getEnabled(true) && !$this->getIsValid();
|
|
|
140 |
if($display===TValidatorDisplayStyle::None || (!$visible && $display===TValidatorDisplayStyle::Dynamic))
|
|
|
141 |
$writer->addStyleAttribute('display','none');
|
|
|
142 |
else if(!$visible)
|
|
|
143 |
$writer->addStyleAttribute('visibility','hidden');
|
|
|
144 |
$writer->addAttribute('id',$this->getClientID());
|
|
|
145 |
parent::addAttributesToRender($writer);
|
|
|
146 |
$this->renderClientControlScript($writer);
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Returns an array of javascript validator options.
|
|
|
151 |
* @return array javascript validator options.
|
|
|
152 |
*/
|
|
|
153 |
protected function getClientScriptOptions()
|
|
|
154 |
{
|
|
|
155 |
$control = $this->getValidationTarget();
|
|
|
156 |
$options['ID'] = $this->getClientID();
|
|
|
157 |
$options['FormID'] = $this->getPage()->getForm()->getClientID();
|
|
|
158 |
$options['Display'] = $this->getDisplay();
|
|
|
159 |
$options['ErrorMessage'] = $this->getErrorMessage();
|
|
|
160 |
if($this->getFocusOnError())
|
|
|
161 |
{
|
|
|
162 |
$options['FocusOnError'] = $this->getFocusOnError();
|
|
|
163 |
$options['FocusElementID'] = $this->getFocusElementID();
|
|
|
164 |
}
|
|
|
165 |
$options['ValidationGroup'] = $this->getValidationGroup();
|
|
|
166 |
$options['ControlToValidate'] = $control->getClientID();
|
|
|
167 |
$options['ControlCssClass'] = $this->getControlCssClass();
|
|
|
168 |
|
|
|
169 |
$options['ControlType'] = $this->getClientControlClass($control);
|
|
|
170 |
|
|
|
171 |
//get date format from date picker target control
|
|
|
172 |
if($control instanceof TDatePicker)
|
|
|
173 |
$options['DateFormat'] = $control->getDateFormat();
|
|
|
174 |
|
|
|
175 |
$options = array_merge($options,$this->getClientSide()->getOptions()->toArray());
|
|
|
176 |
|
|
|
177 |
return $options;
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Gets the Control type for client-side validation. If new cases exists in
|
|
|
182 |
* TBaseValidator::$_clientClass, be sure to update the corresponding
|
|
|
183 |
* "Javascript/validation3.js" file as well.
|
|
|
184 |
* @param TControl control to validate.
|
|
|
185 |
* @return string control type for client-side validation.
|
|
|
186 |
*/
|
|
|
187 |
private function getClientControlClass($control)
|
|
|
188 |
{
|
|
|
189 |
foreach(self::$_clientClass as $type)
|
|
|
190 |
if($control instanceof $type)
|
|
|
191 |
return $type;
|
|
|
192 |
return get_class($control);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* Gets the TValidatorClientSide that allows modification of the client-
|
|
|
197 |
* side validator events.
|
|
|
198 |
*
|
|
|
199 |
* The client-side validator supports the following events.
|
|
|
200 |
* # <tt>OnValidate</tt> -- raised before client-side validation is
|
|
|
201 |
* executed.
|
|
|
202 |
* # <tt>OnValidationSuccess</tt> -- raised after client-side validation is completed
|
|
|
203 |
* and is successfull, overrides default validator error messages updates.
|
|
|
204 |
* # <tt>OnValidationError</tt> -- raised after client-side validation is completed
|
|
|
205 |
* and failed, overrides default validator error message updates.
|
|
|
206 |
*
|
|
|
207 |
* You can attach custom javascript code to each of these events
|
|
|
208 |
*
|
|
|
209 |
* @return TValidatorClientSide javascript validator event options.
|
|
|
210 |
*/
|
|
|
211 |
public function getClientSide()
|
|
|
212 |
{
|
|
|
213 |
if(is_null($this->_clientSide))
|
|
|
214 |
$this->_clientSide = $this->createClientSide();
|
|
|
215 |
return $this->_clientSide;
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
/**
|
|
|
219 |
* @return TValidatorClientSide javascript validator event options.
|
|
|
220 |
*/
|
|
|
221 |
protected function createClientSide()
|
|
|
222 |
{
|
|
|
223 |
return new TValidatorClientSide;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Renders the javascript code to the end script.
|
|
|
228 |
* If you override this method, be sure to call the parent implementation
|
|
|
229 |
* so that the event handlers can be invoked.
|
|
|
230 |
* @param THtmlWriter the renderer
|
|
|
231 |
*/
|
|
|
232 |
public function renderClientControlScript($writer)
|
|
|
233 |
{
|
|
|
234 |
$scripts = $this->getPage()->getClientScript();
|
|
|
235 |
$formID=$this->getPage()->getForm()->getClientID();
|
|
|
236 |
$scriptKey = "TBaseValidator:$formID";
|
|
|
237 |
if($this->getEnableClientScript() && !$scripts->isEndScriptRegistered($scriptKey))
|
|
|
238 |
{
|
|
|
239 |
$manager['FormID'] = $formID;
|
|
|
240 |
$options = TJavaScript::encode($manager);
|
|
|
241 |
$scripts->registerPradoScript('validator');
|
|
|
242 |
$scripts->registerEndScript($scriptKey, "new Prado.ValidationManager({$options});");
|
|
|
243 |
}
|
|
|
244 |
if($this->getEnableClientScript() & $this->getEnabled(true))
|
|
|
245 |
$this->registerClientScriptValidator();
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* Override parent implementation to update the control CSS Class before
|
|
|
250 |
* the validated control is rendered
|
|
|
251 |
*/
|
|
|
252 |
public function onPreRender ($param)
|
|
|
253 |
{
|
|
|
254 |
parent::onPreRender($param);
|
|
|
255 |
$this->updateControlCssClass();
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
/**
|
|
|
259 |
* Update the ControlToValidate component's css class depending
|
|
|
260 |
* if the ControlCssClass property is set, and whether this is valid.
|
|
|
261 |
* @return boolean true if change, false otherwise.
|
|
|
262 |
*/
|
|
|
263 |
protected function updateControlCssClass()
|
|
|
264 |
{
|
|
|
265 |
if(($cssClass=$this->getControlCssClass())!=='')
|
|
|
266 |
{
|
|
|
267 |
$control=$this->getValidationTarget();
|
|
|
268 |
if($control instanceof TWebControl)
|
|
|
269 |
{
|
|
|
270 |
$class = preg_replace ('/ '.preg_quote($cssClass).'/', '',$control->getCssClass());
|
|
|
271 |
if(!$this->getIsValid())
|
|
|
272 |
{
|
|
|
273 |
$class .= ' '.$cssClass;
|
|
|
274 |
$control->setCssClass($class);
|
|
|
275 |
} elseif ($control->getIsValid())
|
|
|
276 |
$control->setCssClass($class);
|
|
|
277 |
}
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* Registers the individual validator client-side javascript code.
|
|
|
283 |
*/
|
|
|
284 |
protected function registerClientScriptValidator()
|
|
|
285 |
{
|
|
|
286 |
$key = 'prado:'.$this->getClientID();
|
|
|
287 |
if(!$this->getPage()->getClientScript()->isEndScriptRegistered($key))
|
|
|
288 |
{
|
|
|
289 |
$options = TJavaScript::encode($this->getClientScriptOptions());
|
|
|
290 |
$script = 'new '.$this->getClientClassName().'('.$options.');';
|
|
|
291 |
$this->getPage()->getClientScript()->registerEndScript($key, $script);
|
|
|
292 |
}
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* Gets the name of the javascript class responsible for performing validation for this control.
|
|
|
297 |
* This method overrides the parent implementation.
|
|
|
298 |
* @return string the javascript class name
|
|
|
299 |
*/
|
|
|
300 |
abstract protected function getClientClassName();
|
|
|
301 |
|
|
|
302 |
/**
|
|
|
303 |
* This method overrides the parent implementation to forbid setting ForControl.
|
|
|
304 |
* @param string the associated control ID
|
|
|
305 |
* @throws TNotSupportedException whenever this method is called
|
|
|
306 |
*/
|
|
|
307 |
public function setForControl($value)
|
|
|
308 |
{
|
|
|
309 |
throw new TNotSupportedException('basevalidator_forcontrol_unsupported',get_class($this));
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
/**
|
|
|
313 |
* This method overrides parent's implementation by setting {@link setIsValid IsValid} to true if disabled.
|
|
|
314 |
* @param boolean whether the validator is enabled.
|
|
|
315 |
*/
|
|
|
316 |
public function setEnabled($value)
|
|
|
317 |
{
|
|
|
318 |
$value=TPropertyValue::ensureBoolean($value);
|
|
|
319 |
parent::setEnabled($value);
|
|
|
320 |
if(!$value)
|
|
|
321 |
$this->_isValid=true;
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
/**
|
|
|
325 |
* @return TValidatorDisplayStyle the style of displaying the error message. Defaults to TValidatorDisplayStyle::Fixed.
|
|
|
326 |
*/
|
|
|
327 |
public function getDisplay()
|
|
|
328 |
{
|
|
|
329 |
return $this->getViewState('Display',TValidatorDisplayStyle::Fixed);
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
/**
|
|
|
333 |
* @param TValidatorDisplayStyle the style of displaying the error message
|
|
|
334 |
*/
|
|
|
335 |
public function setDisplay($value)
|
|
|
336 |
{
|
|
|
337 |
$this->setViewState('Display',TPropertyValue::ensureEnum($value,'TValidatorDisplayStyle'),TValidatorDisplayStyle::Fixed);
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
/**
|
|
|
341 |
* @return boolean whether client-side validation is enabled.
|
|
|
342 |
*/
|
|
|
343 |
public function getEnableClientScript()
|
|
|
344 |
{
|
|
|
345 |
return $this->getViewState('EnableClientScript',true);
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
/**
|
|
|
349 |
* @param boolean whether client-side validation is enabled.
|
|
|
350 |
*/
|
|
|
351 |
public function setEnableClientScript($value)
|
|
|
352 |
{
|
|
|
353 |
$this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
/**
|
|
|
357 |
* @return string the text for the error message.
|
|
|
358 |
*/
|
|
|
359 |
public function getErrorMessage()
|
|
|
360 |
{
|
|
|
361 |
return $this->getViewState('ErrorMessage','');
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
/**
|
|
|
365 |
* Sets the text for the error message.
|
|
|
366 |
* @param string the error message
|
|
|
367 |
*/
|
|
|
368 |
public function setErrorMessage($value)
|
|
|
369 |
{
|
|
|
370 |
$this->setViewState('ErrorMessage',$value,'');
|
|
|
371 |
}
|
|
|
372 |
|
|
|
373 |
/**
|
|
|
374 |
* @return string the ID path of the input control to validate
|
|
|
375 |
*/
|
|
|
376 |
public function getControlToValidate()
|
|
|
377 |
{
|
|
|
378 |
return $this->getViewState('ControlToValidate','');
|
|
|
379 |
}
|
|
|
380 |
|
|
|
381 |
/**
|
|
|
382 |
* Sets the ID path of the input control to validate.
|
|
|
383 |
* The ID path is the dot-connected IDs of the controls reaching from
|
|
|
384 |
* the validator's naming container to the target control.
|
|
|
385 |
* @param string the ID path
|
|
|
386 |
*/
|
|
|
387 |
public function setControlToValidate($value)
|
|
|
388 |
{
|
|
|
389 |
$this->setViewState('ControlToValidate',$value,'');
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
/**
|
|
|
393 |
* @return boolean whether to set focus at the validating place if the validation fails. Defaults to false.
|
|
|
394 |
*/
|
|
|
395 |
public function getFocusOnError()
|
|
|
396 |
{
|
|
|
397 |
return $this->getViewState('FocusOnError',false);
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
/**
|
|
|
401 |
* @param boolean whether to set focus at the validating place if the validation fails
|
|
|
402 |
*/
|
|
|
403 |
public function setFocusOnError($value)
|
|
|
404 |
{
|
|
|
405 |
$this->setViewState('FocusOnError',TPropertyValue::ensureBoolean($value),false);
|
|
|
406 |
}
|
|
|
407 |
|
|
|
408 |
/**
|
|
|
409 |
* Gets the ID of the HTML element that will receive focus if validation fails and {@link getFocusOnError FocusOnError} is true.
|
|
|
410 |
* Defaults to the client ID of the {@link getControlToValidate ControlToValidate}.
|
|
|
411 |
* @return string the ID of the HTML element to receive focus
|
|
|
412 |
*/
|
|
|
413 |
public function getFocusElementID()
|
|
|
414 |
{
|
|
|
415 |
if(($id=$this->getViewState('FocusElementID',''))==='')
|
|
|
416 |
$id=$this->getValidationTarget()->getClientID();
|
|
|
417 |
return $id;
|
|
|
418 |
}
|
|
|
419 |
|
|
|
420 |
/**
|
|
|
421 |
* Sets the ID of the HTML element that will receive focus if validation fails and {@link getFocusOnError FocusOnError} is true.
|
|
|
422 |
* @param string the ID of the HTML element to receive focus
|
|
|
423 |
*/
|
|
|
424 |
public function setFocusElementID($value)
|
|
|
425 |
{
|
|
|
426 |
$this->setViewState('FocusElementID', $value, '');
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
/**
|
|
|
430 |
* @return string the group which this validator belongs to
|
|
|
431 |
*/
|
|
|
432 |
public function getValidationGroup()
|
|
|
433 |
{
|
|
|
434 |
return $this->getViewState('ValidationGroup','');
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
/**
|
|
|
438 |
* @param string the group which this validator belongs to
|
|
|
439 |
*/
|
|
|
440 |
public function setValidationGroup($value)
|
|
|
441 |
{
|
|
|
442 |
$this->setViewState('ValidationGroup',$value,'');
|
|
|
443 |
}
|
|
|
444 |
|
|
|
445 |
/**
|
|
|
446 |
* @return boolean whether the validation succeeds
|
|
|
447 |
*/
|
|
|
448 |
public function getIsValid()
|
|
|
449 |
{
|
|
|
450 |
return $this->_isValid;
|
|
|
451 |
}
|
|
|
452 |
|
|
|
453 |
/**
|
|
|
454 |
* Sets the value indicating whether the validation succeeds
|
|
|
455 |
* @param boolean whether the validation succeeds
|
|
|
456 |
*/
|
|
|
457 |
public function setIsValid($value)
|
|
|
458 |
{
|
|
|
459 |
$this->_isValid=TPropertyValue::ensureBoolean($value);
|
|
|
460 |
}
|
|
|
461 |
|
|
|
462 |
/**
|
|
|
463 |
* @return TControl control to be validated. Null if no control is found.
|
|
|
464 |
* @throws TConfigurationException if {@link getControlToValidate
|
|
|
465 |
* ControlToValidate} is empty or does not point to a valid control
|
|
|
466 |
*/
|
|
|
467 |
protected function getValidationTarget()
|
|
|
468 |
{
|
|
|
469 |
if(($id=$this->getControlToValidate())!=='' && ($control=$this->findControl($id))!==null)
|
|
|
470 |
return $control;
|
|
|
471 |
else
|
|
|
472 |
throw new TConfigurationException('basevalidator_controltovalidate_invalid',get_class($this));
|
|
|
473 |
}
|
|
|
474 |
|
|
|
475 |
/**
|
|
|
476 |
* Retrieves the property value of the control being validated.
|
|
|
477 |
* @param TControl control being validated
|
|
|
478 |
* @return string property value to be validated
|
|
|
479 |
* @throws TInvalidDataTypeException if the control to be validated does not implement {@link IValidatable}.
|
|
|
480 |
*/
|
|
|
481 |
protected function getValidationValue($control)
|
|
|
482 |
{
|
|
|
483 |
if($control instanceof IValidatable)
|
|
|
484 |
return $control->getValidationPropertyValue();
|
|
|
485 |
else
|
|
|
486 |
throw new TInvalidDataTypeException('basevalidator_validatable_required',get_class($this));
|
|
|
487 |
}
|
|
|
488 |
|
|
|
489 |
/**
|
|
|
490 |
* Validates the specified control.
|
|
|
491 |
* Do not override this method. Override {@link evaluateIsValid} instead.
|
|
|
492 |
* @return boolean whether the validation succeeds
|
|
|
493 |
*/
|
|
|
494 |
public function validate()
|
|
|
495 |
{
|
|
|
496 |
$this->setIsValid(true);
|
|
|
497 |
$this->onValidate();
|
|
|
498 |
if($this->getVisible(true) && $this->getEnabled(true))
|
|
|
499 |
{
|
|
|
500 |
// if the target is not a disabled web control
|
|
|
501 |
if(($target=$this->getValidationTarget())!==null && !($target instanceof TWebControl && !$target->getEnabled(true)))
|
|
|
502 |
{
|
|
|
503 |
if($this->evaluateIsValid())
|
|
|
504 |
{
|
|
|
505 |
$this->setIsValid(true);
|
|
|
506 |
$this->onValidationSuccess();
|
|
|
507 |
}
|
|
|
508 |
else
|
|
|
509 |
{
|
|
|
510 |
$target->setIsValid(false);
|
|
|
511 |
$this->setIsValid(false);
|
|
|
512 |
$this->onValidationError();
|
|
|
513 |
}
|
|
|
514 |
}
|
|
|
515 |
else
|
|
|
516 |
{
|
|
|
517 |
$this->evaluateIsValid();
|
|
|
518 |
$this->setIsValid(true);
|
|
|
519 |
$this->onValidationSuccess();
|
|
|
520 |
}
|
|
|
521 |
}
|
|
|
522 |
return $this->getIsValid();
|
|
|
523 |
}
|
|
|
524 |
|
|
|
525 |
/**
|
|
|
526 |
* @return string the css class that is applied to the control being validated in case the validation fails
|
|
|
527 |
*/
|
|
|
528 |
public function getControlCssClass()
|
|
|
529 |
{
|
|
|
530 |
return $this->getViewState('ControlCssClass','');
|
|
|
531 |
}
|
|
|
532 |
|
|
|
533 |
/**
|
|
|
534 |
* @param string the css class that is applied to the control being validated in case the validation fails
|
|
|
535 |
*/
|
|
|
536 |
public function setControlCssClass($value)
|
|
|
537 |
{
|
|
|
538 |
$this->setViewState('ControlCssClass',$value,'');
|
|
|
539 |
}
|
|
|
540 |
|
|
|
541 |
/**
|
|
|
542 |
* This is the major method for validation.
|
|
|
543 |
* Derived classes should implement this method to provide customized validation.
|
|
|
544 |
* @return boolean whether the validation succeeds
|
|
|
545 |
*/
|
|
|
546 |
abstract protected function evaluateIsValid();
|
|
|
547 |
|
|
|
548 |
/**
|
|
|
549 |
* This event is raised when the validator succeeds in validation.
|
|
|
550 |
*/
|
|
|
551 |
public function onValidationSuccess()
|
|
|
552 |
{
|
|
|
553 |
$this->raiseEvent('OnValidationSuccess',$this,null);
|
|
|
554 |
}
|
|
|
555 |
|
|
|
556 |
/**
|
|
|
557 |
* This event is raised when the validator fails in validation.
|
|
|
558 |
*/
|
|
|
559 |
public function onValidationError()
|
|
|
560 |
{
|
|
|
561 |
$this->raiseEvent('OnValidationError',$this,null);
|
|
|
562 |
}
|
|
|
563 |
|
|
|
564 |
/**
|
|
|
565 |
* This event is raised right before the validator starts to perform validation.
|
|
|
566 |
* You may use this event to change the behavior of validation.
|
|
|
567 |
* For example, you may disable the validator if certain condition is satisfied.
|
|
|
568 |
* Note, the event will NOT be raised if the validator is invisible.
|
|
|
569 |
*/
|
|
|
570 |
public function onValidate()
|
|
|
571 |
{
|
|
|
572 |
$this->raiseEvent('OnValidate',$this,null);
|
|
|
573 |
}
|
|
|
574 |
|
|
|
575 |
/**
|
|
|
576 |
* Renders the validator control.
|
|
|
577 |
* @param THtmlWriter writer for the rendering purpose
|
|
|
578 |
*/
|
|
|
579 |
public function renderContents($writer)
|
|
|
580 |
{
|
|
|
581 |
if(($text=$this->getText())!=='')
|
|
|
582 |
$writer->write($text);
|
|
|
583 |
else if(($text=$this->getErrorMessage())!=='')
|
|
|
584 |
$writer->write($text);
|
|
|
585 |
else
|
|
|
586 |
parent::renderContents($writer);
|
|
|
587 |
}
|
|
|
588 |
}
|
|
|
589 |
|
|
|
590 |
/**
|
|
|
591 |
* TValidatorClientSide class.
|
|
|
592 |
*
|
|
|
593 |
* Client-side validator events can be modified through the {@link
|
|
|
594 |
* TBaseValidator::getClientSide ClientSide} property of a validator. The
|
|
|
595 |
* subproperties of ClientSide are those of the TValidatorClientSide
|
|
|
596 |
* properties. The client-side validator supports the following events.
|
|
|
597 |
*
|
|
|
598 |
* The <tt>OnValidate</tt> event is raise before the validator validation
|
|
|
599 |
* functions are called.
|
|
|
600 |
*
|
|
|
601 |
* The <tt>OnValidationSuccess</tt> event is raised after the validator has successfully
|
|
|
602 |
* validate the control.
|
|
|
603 |
*
|
|
|
604 |
* The <tt>OnValidationError</tt> event is raised after the validator fails validation.
|
|
|
605 |
*
|
|
|
606 |
* See the quickstart documentation for further details.
|
|
|
607 |
*
|
|
|
608 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
609 |
* @version $Id: TBaseValidator.php 2551 2008-10-30 17:07:05Z carlgmathisen $
|
|
|
610 |
* @package System.Web.UI.WebControls
|
|
|
611 |
* @since 3.0
|
|
|
612 |
*/
|
|
|
613 |
class TValidatorClientSide extends TClientSideOptions
|
|
|
614 |
{
|
|
|
615 |
/**
|
|
|
616 |
* @return string javascript code for client-side OnValidate event.
|
|
|
617 |
*/
|
|
|
618 |
public function getOnValidate()
|
|
|
619 |
{
|
|
|
620 |
return $this->getOption('OnValidate');
|
|
|
621 |
}
|
|
|
622 |
|
|
|
623 |
/**
|
|
|
624 |
* Client-side OnValidate validator event is raise before the validators
|
|
|
625 |
* validation functions are called.
|
|
|
626 |
* @param string javascript code for client-side OnValidate event.
|
|
|
627 |
*/
|
|
|
628 |
public function setOnValidate($javascript)
|
|
|
629 |
{
|
|
|
630 |
$this->setFunction('OnValidate', $javascript);
|
|
|
631 |
}
|
|
|
632 |
|
|
|
633 |
/**
|
|
|
634 |
* Client-side OnSuccess event is raise after validation is successfull.
|
|
|
635 |
* This will override the default client-side validator behaviour.
|
|
|
636 |
* @param string javascript code for client-side OnSuccess event.
|
|
|
637 |
*/
|
|
|
638 |
public function setOnValidationSuccess($javascript)
|
|
|
639 |
{
|
|
|
640 |
$this->setFunction('OnValidationSuccess', $javascript);
|
|
|
641 |
}
|
|
|
642 |
|
|
|
643 |
/**
|
|
|
644 |
* @return string javascript code for client-side OnSuccess event.
|
|
|
645 |
*/
|
|
|
646 |
public function getOnValidationSuccess()
|
|
|
647 |
{
|
|
|
648 |
return $this->getOption('OnValidationSuccess');
|
|
|
649 |
}
|
|
|
650 |
|
|
|
651 |
/**
|
|
|
652 |
* Client-side OnError event is raised after validation failure.
|
|
|
653 |
* This will override the default client-side validator behaviour.
|
|
|
654 |
* @param string javascript code for client-side OnError event.
|
|
|
655 |
*/
|
|
|
656 |
public function setOnValidationError($javascript)
|
|
|
657 |
{
|
|
|
658 |
$this->setFunction('OnValidationError', $javascript);
|
|
|
659 |
}
|
|
|
660 |
|
|
|
661 |
/**
|
|
|
662 |
* @return string javascript code for client-side OnError event.
|
|
|
663 |
*/
|
|
|
664 |
public function getOnValidationError()
|
|
|
665 |
{
|
|
|
666 |
return $this->getOption('OnValidationError');
|
|
|
667 |
}
|
|
|
668 |
|
|
|
669 |
/**
|
|
|
670 |
* @param boolean true to revalidate when the control to validate changes value.
|
|
|
671 |
*/
|
|
|
672 |
public function setObserveChanges($value)
|
|
|
673 |
{
|
|
|
674 |
$this->setOption('ObserveChanges', TPropertyValue::ensureBoolean($value));
|
|
|
675 |
}
|
|
|
676 |
|
|
|
677 |
/**
|
|
|
678 |
* @return boolean true to observe changes.
|
|
|
679 |
*/
|
|
|
680 |
public function getObserveChanges()
|
|
|
681 |
{
|
|
|
682 |
$changes = $this->getOption('ObserveChanges');
|
|
|
683 |
return is_null($changes) ? true : $changes;
|
|
|
684 |
}
|
|
|
685 |
}
|
|
|
686 |
|
|
|
687 |
|
|
|
688 |
/**
|
|
|
689 |
* TValidatorDisplayStyle class.
|
|
|
690 |
* TValidatorDisplayStyle defines the enumerable type for the possible styles
|
|
|
691 |
* that a validator control can display the error message.
|
|
|
692 |
*
|
|
|
693 |
* The following enumerable values are defined:
|
|
|
694 |
* - None: the error message is not displayed
|
|
|
695 |
* - Dynamic: the error message dynamically appears when the validator fails validation
|
|
|
696 |
* - Fixed: Similar to Dynamic except that the error message physically occupies the page layout (even though it may not be visible)
|
|
|
697 |
*
|
|
|
698 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
699 |
* @version $Id: TBaseValidator.php 2551 2008-10-30 17:07:05Z carlgmathisen $
|
|
|
700 |
* @package System.Web.UI.WebControls
|
|
|
701 |
* @since 3.0.4
|
|
|
702 |
*/
|
|
|
703 |
class TValidatorDisplayStyle extends TEnumerable
|
|
|
704 |
{
|
|
|
705 |
const None='None';
|
|
|
706 |
const Dynamic='Dynamic';
|
|
|
707 |
const Fixed='Fixed';
|
|
|
708 |
}
|
|
|
709 |
|
|
|
710 |
/**
|
|
|
711 |
* TValidationDataType class.
|
|
|
712 |
* TValidationDataType defines the enumerable type for the possible data types that
|
|
|
713 |
* a comparison validator can validate upon.
|
|
|
714 |
*
|
|
|
715 |
* The following enumerable values are defined:
|
|
|
716 |
* - Integer
|
|
|
717 |
* - Float
|
|
|
718 |
* - Date
|
|
|
719 |
* - String
|
|
|
720 |
*
|
|
|
721 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
722 |
* @version $Id: TBaseValidator.php 2551 2008-10-30 17:07:05Z carlgmathisen $
|
|
|
723 |
* @package System.Web.UI.WebControls
|
|
|
724 |
* @since 3.0.4
|
|
|
725 |
*/
|
|
|
726 |
class TValidationDataType extends TEnumerable
|
|
|
727 |
{
|
|
|
728 |
const Integer='Integer';
|
|
|
729 |
const Float='Float';
|
|
|
730 |
const Date='Date';
|
|
|
731 |
const String='String';
|
|
|
732 |
}
|
|
|
733 |
|