3 * TActiveCustomValidator class file.
5 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Web.UI.ActiveControls
12 Prado::using('System.Web.UI.ActiveControls.TCallbackClientSide');
15 * TActiveCustomValidator Class
17 * Performs custom validation using only server-side {@link onServerValidate onServerValidate}
18 * validation event. The client-side uses callbacks to raise
19 * the {@link onServerValidate onServerValidate} event.
21 * Beware that the {@link onServerValidate onServerValidate} may be
22 * raised when the control to validate on the client side
23 * changes value, that is, the server validation may be called many times.
25 * After the callback or postback, the {@link onServerValidate onServerValidate}
26 * is raised once more. The {@link getIsCallback IsCallback} property
27 * will be true when validation is made during a callback request.
29 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
30 * @package System.Web.UI.ActiveControls
33 class TActiveCustomValidator extends TCustomValidator
34 implements ICallbackEventHandler, IActiveControl
37 * @var boolean true if validation is made during a callback request.
39 private $_isCallback = false;
42 * @return boolean true if validation is made during a callback request.
44 public function getIsCallback()
46 return $this->_isCallback;
50 * Creates a new callback control, sets the adapter to
51 * TActiveControlAdapter. If you override this class, be sure to set the
52 * adapter appropriately by, for example, by calling this constructor.
54 public function __construct()
56 parent::__construct();
57 $this->setAdapter(new TActiveControlAdapter($this));
58 $this->getActiveControl()->setClientSide(new TActiveCustomValidatorClientSide);
62 * @return TBaseActiveCallbackControl standard callback control options.
64 public function getActiveControl()
66 return $this->getAdapter()->getBaseActiveControl();
70 * @return TCallbackClientSide client side request options.
72 public function getClientSide()
74 return $this->getAdapter()->getBaseActiveControl()->getClientSide();
78 * Client validation function is NOT supported.
80 public function setClientValidationFunction($value)
82 throw new TNotSupportedException('tactivecustomvalidator_clientfunction_unsupported',
87 * Raises the callback event. This method is required by {@link
88 * ICallbackEventHandler} interface. The {@link onServerValidate
89 * OnServerValidate} event is raised first and then the
90 * {@link onCallback OnCallback} event.
91 * This method is mainly used by framework and control developers.
92 * @param TCallbackEventParameter the event parameter
94 public function raiseCallbackEvent($param)
96 $this->_isCallback = true;
97 $result = $this->onServerValidate($param->getCallbackParameter());
98 $param->setResponseData($result);
99 $this->onCallback($param);
103 * @param boolean whether the value is valid; this method will trigger a clientside update if needed
105 public function setIsValid($value)
107 // Always update the clientside, since the clientside's value for IsValid
108 // it could have been changed by the clientside validation.
110 parent::setIsValid($value);
111 if($this->getActiveControl()->canUpdateClientSide())
113 $client = $this->getPage()->getCallbackClient();
114 $func = 'Prado.Validation.updateActiveCustomValidator';
115 $client->callClientFunction($func, array($this, $value));
120 * This method is invoked when a callback is requested. The method raises
121 * 'OnCallback' event to fire up the event handlers. If you override this
122 * method, be sure to call the parent implementation so that the event
123 * handler can be invoked.
124 * @param TCallbackEventParameter event parameter to be passed to the event handlers
126 public function onCallback($param)
128 $this->raiseEvent('OnCallback', $this, $param);
132 * Returns an array of javascript validator options.
133 * @return array javascript validator options.
135 protected function getClientScriptOptions()
137 $options=TBaseValidator::getClientScriptOptions();
138 $options['EventTarget'] = $this->getUniqueID();
143 * Sets the text for the error message. Updates client-side error message.
144 * @param string the error message
146 public function setErrorMessage($value)
148 if(parent::getErrorMessage() === $value)
152 parent::setErrorMessage($value);
153 if($this->getActiveControl()->canUpdateClientSide())
155 $client = $this->getPage()->getCallbackClient();
156 $func = 'Prado.Validation.setErrorMessage';
157 $client->callClientFunction($func, array($this, $value));
163 * It's mandatory for the EnableClientScript to be activated or the TActiveCustomValidator won't work.
164 * @return boolean whether client-side validation is enabled.
166 public function getEnableClientScript()
172 * Ensure that the ID attribute is rendered and registers the javascript code
173 * for initializing the active control.
175 protected function addAttributesToRender($writer)
177 parent::addAttributesToRender($writer);
178 TBaseValidator::registerClientScriptValidator();
182 * @return string corresponding javascript class name for this this.
184 protected function getClientClassName()
186 return 'Prado.WebUI.TActiveCustomValidator';
191 * Custom Validator callback client side options class.
193 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
194 * @package System.Web.UI.ActiveControls
197 class TActiveCustomValidatorClientSide extends TCallbackClientSide
200 * @return string javascript code for client-side OnValidate event.
202 public function getOnValidate()
204 return $this->getOption('OnValidate');
208 * Client-side OnValidate validator event is raise before the validators
209 * validation functions are called.
210 * @param string javascript code for client-side OnValidate event.
212 public function setOnValidate($javascript)
214 $this->setFunction('OnValidate', $javascript);
218 * Client-side OnSuccess event is raise after validation is successfull.
219 * This will override the default client-side validator behaviour.
220 * @param string javascript code for client-side OnSuccess event.
222 public function setOnValidationSuccess($javascript)
224 $this->setFunction('OnValidationSuccess', $javascript);
228 * @return string javascript code for client-side OnSuccess event.
230 public function getOnValidationSuccess()
232 return $this->getOption('OnValidationSuccess');
236 * Client-side OnError event is raised after validation failure.
237 * This will override the default client-side validator behaviour.
238 * @param string javascript code for client-side OnError event.
240 public function setOnValidationError($javascript)
242 $this->setFunction('OnValidationError', $javascript);
246 * @return string javascript code for client-side OnError event.
248 public function getOnValidationError()
250 return $this->getOption('OnValidationError');
254 * @param boolean true to revalidate when the control to validate changes value.
256 public function setObserveChanges($value)
258 $this->setOption('ObserveChanges', TPropertyValue::ensureBoolean($value));
262 * @return boolean true to observe changes.
264 public function getObserveChanges()
266 $changes = $this->getOption('ObserveChanges');
267 return ($changes===null) ? true : $changes;