]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/UI/ActiveControls/TActiveCustomValidator.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / UI / ActiveControls / TActiveCustomValidator.php
1 <?php
2 /**
3  * TActiveCustomValidator class file.
4  *
5  * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Web.UI.ActiveControls
10  */
11
12 Prado::using('System.Web.UI.ActiveControls.TCallbackClientSide');
13
14 /**
15  * TActiveCustomValidator Class
16  *
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.
20  *
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.
24  *
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.
28  *
29  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
30  * @package System.Web.UI.ActiveControls
31  * @since 3.1
32  */
33 class TActiveCustomValidator extends TCustomValidator
34         implements ICallbackEventHandler, IActiveControl
35 {
36         /**
37          * @var boolean true if validation is made during a callback request.
38          */
39         private $_isCallback = false;
40
41         /**
42          * @return boolean true if validation is made during a callback request.
43          */
44         public function getIsCallback()
45         {
46                 return $this->_isCallback;
47         }
48
49         /**
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.
53          */
54         public function __construct()
55         {
56                 parent::__construct();
57                 $this->setAdapter(new TActiveControlAdapter($this));
58                 $this->getActiveControl()->setClientSide(new TActiveCustomValidatorClientSide);
59         }
60
61         /**
62          * @return TBaseActiveCallbackControl standard callback control options.
63          */
64         public function getActiveControl()
65         {
66                 return $this->getAdapter()->getBaseActiveControl();
67         }
68
69         /**
70          * @return TCallbackClientSide client side request options.
71          */
72         public function getClientSide()
73         {
74                 return $this->getAdapter()->getBaseActiveControl()->getClientSide();
75         }
76
77         /**
78          * Client validation function is NOT supported.
79          */
80         public function setClientValidationFunction($value)
81         {
82                 throw new TNotSupportedException('tactivecustomvalidator_clientfunction_unsupported',
83                         get_class($this));
84         }
85
86         /**
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
93          */
94         public function raiseCallbackEvent($param)
95         {
96                 $this->_isCallback = true;
97                 $result = $this->onServerValidate($param->getCallbackParameter());
98                 $param->setResponseData($result);
99                 $this->onCallback($param);
100         }
101
102         /**
103          * @param boolean whether the value is valid; this method will trigger a clientside update if needed
104          */
105         public function setIsValid($value)
106         {
107                 // Always update the clientside, since the clientside's value for IsValid
108                 // it could have been changed by the clientside validation.
109
110                 parent::setIsValid($value);
111                 if($this->getActiveControl()->canUpdateClientSide())
112                 {
113                         $client = $this->getPage()->getCallbackClient();
114                         $func = 'Prado.Validation.updateActiveCustomValidator';
115                         $client->callClientFunction($func, array($this, $value));
116                 }
117         }
118
119         /**
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
125          */
126         public function onCallback($param)
127         {
128                 $this->raiseEvent('OnCallback', $this, $param);
129         }
130
131         /**
132          * Returns an array of javascript validator options.
133          * @return array javascript validator options.
134          */
135         protected function getClientScriptOptions()
136         {
137                 $options=TBaseValidator::getClientScriptOptions();
138                 $options['EventTarget'] = $this->getUniqueID();
139                 return $options;
140         }
141
142         /**
143          * Sets the text for the error message. Updates client-side error message.
144          * @param string the error message
145          */
146         public function setErrorMessage($value)
147         {
148                 if(parent::getErrorMessage() === $value)
149                         return;
150
151
152                 parent::setErrorMessage($value);
153                 if($this->getActiveControl()->canUpdateClientSide())
154                 {
155                         $client = $this->getPage()->getCallbackClient();
156                         $func = 'Prado.Validation.setErrorMessage';
157                         $client->callClientFunction($func, array($this, $value));
158                 }
159         }
160
161
162         /**
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.
165          */
166         public function getEnableClientScript()
167         {
168                 return true;
169         }
170
171         /**
172          * Ensure that the ID attribute is rendered and registers the javascript code
173          * for initializing the active control.
174          */
175         protected function addAttributesToRender($writer)
176         {
177                 parent::addAttributesToRender($writer);
178                 TBaseValidator::registerClientScriptValidator();
179         }
180
181         /**
182          * @return string corresponding javascript class name for this this.
183          */
184         protected function getClientClassName()
185         {
186                 return 'Prado.WebUI.TActiveCustomValidator';
187         }
188 }
189
190 /**
191  * Custom Validator callback client side options class.
192  *
193  * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
194  * @package System.Web.UI.ActiveControls
195  * @since 3.1
196  */
197 class TActiveCustomValidatorClientSide extends TCallbackClientSide
198 {
199         /**
200          * @return string javascript code for client-side OnValidate event.
201          */
202         public function getOnValidate()
203         {
204                 return $this->getOption('OnValidate');
205         }
206
207         /**
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.
211          */
212         public function setOnValidate($javascript)
213         {
214                 $this->setFunction('OnValidate', $javascript);
215         }
216
217         /**
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.
221          */
222         public function setOnValidationSuccess($javascript)
223         {
224                 $this->setFunction('OnValidationSuccess', $javascript);
225         }
226
227         /**
228          * @return string javascript code for client-side OnSuccess event.
229          */
230         public function getOnValidationSuccess()
231         {
232                 return $this->getOption('OnValidationSuccess');
233         }
234
235         /**
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.
239          */
240         public function setOnValidationError($javascript)
241         {
242                 $this->setFunction('OnValidationError', $javascript);
243         }
244
245         /**
246          * @return string javascript code for client-side OnError event.
247          */
248         public function getOnValidationError()
249         {
250                 return $this->getOption('OnValidationError');
251         }
252
253         /**
254          * @param boolean true to revalidate when the control to validate changes value.
255          */
256         public function setObserveChanges($value)
257         {
258                 $this->setOption('ObserveChanges', TPropertyValue::ensureBoolean($value));
259         }
260
261         /**
262          * @return boolean true to observe changes.
263          */
264         public function getObserveChanges()
265         {
266                 $changes = $this->getOption('ObserveChanges');
267                 return ($changes===null) ? true : $changes;
268         }
269 }