Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TActiveCustomValidator class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TActiveCustomValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI.ActiveControls
11
 */
12
 
13
Prado::using('System.Web.UI.ActiveControls.TCallbackClientSide');
14
 
15
/**
16
 * TActiveCustomValidator Class
17
 *
18
 * Performs custom validation using only server-side {@link onServerValidate onServerValidate}
19
 * validation event. The client-side uses callbacks to raise
20
 * the {@link onServerValidate onServerValidate} event.
21
 *
22
 * Beware that the {@link onServerValidate onServerValidate} may be
23
 * raised when the control to validate on the client side
24
 * changes value, that is, the server validation may be called many times.
25
 *
26
 * After the callback or postback, the {@link onServerValidate onServerValidate}
27
 * is raised once more. The {@link getIsCallback IsCallback} property
28
 * will be true when validation is made during a callback request.
29
 *
30
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
31
 * @version $Id: TActiveCustomValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
32
 * @package System.Web.UI.ActiveControls
33
 * @since 3.1
34
 */
35
class TActiveCustomValidator extends TCustomValidator
36
	implements ICallbackEventHandler, IActiveControl
37
{
38
	/**
39
	 * @var boolean true if validation is made during a callback request.
40
	 */
41
	private $_isCallback = false;
42
 
43
	/**
44
	 * @return boolean true if validation is made during a callback request.
45
	 */
46
	public function getIsCallback()
47
	{
48
		return $this->_isCallback;
49
	}
50
 
51
	/**
52
	 * Creates a new callback control, sets the adapter to
53
	 * TActiveControlAdapter. If you override this class, be sure to set the
54
	 * adapter appropriately by, for example, by calling this constructor.
55
	 */
56
	public function __construct()
57
	{
58
		parent::__construct();
59
		$this->setAdapter(new TActiveControlAdapter($this));
60
		$this->getActiveControl()->setClientSide(new TActiveCustomValidatorClientSide);
61
	}
62
 
63
	/**
64
	 * @return TBaseActiveCallbackControl standard callback control options.
65
	 */
66
	public function getActiveControl()
67
	{
68
		return $this->getAdapter()->getBaseActiveControl();
69
	}
70
 
71
	/**
72
	 * @return TCallbackClientSide client side request options.
73
	 */
74
	public function getClientSide()
75
	{
76
		return $this->getAdapter()->getBaseActiveControl()->getClientSide();
77
	}
78
 
79
	/**
80
	 * Client validation function is NOT supported.
81
	 */
82
	public function setClientValidationFunction($value)
83
	{
84
		throw new TNotSupportedException('tactivecustomvalidator_clientfunction_unsupported',
85
			get_class($this));
86
	}
87
 
88
	/**
89
	 * Raises the callback event. This method is required by {@link
90
	 * ICallbackEventHandler} interface. The {@link onServerValidate
91
	 * OnServerValidate} event is raised first and then the
92
	 * {@link onCallback OnCallback} event.
93
	 * This method is mainly used by framework and control developers.
94
	 * @param TCallbackEventParameter the event parameter
95
	 */
96
 	public function raiseCallbackEvent($param)
97
	{
98
		$this->_isCallback = true;
99
		$result = $this->onServerValidate($param->getCallbackParameter());
100
		$param->setResponseData($result);
101
		$this->onCallback($param);
102
	}
103
 
104
	/**
105
	 * This method is invoked when a callback is requested. The method raises
106
	 * 'OnCallback' event to fire up the event handlers. If you override this
107
	 * method, be sure to call the parent implementation so that the event
108
	 * handler can be invoked.
109
	 * @param TCallbackEventParameter event parameter to be passed to the event handlers
110
	 */
111
	public function onCallback($param)
112
	{
113
		$this->raiseEvent('OnCallback', $this, $param);
114
	}
115
 
116
	/**
117
	 * Returns an array of javascript validator options.
118
	 * @return array javascript validator options.
119
	 */
120
	protected function getClientScriptOptions()
121
	{
122
		$options=TBaseValidator::getClientScriptOptions();
123
		$options['EventTarget'] = $this->getUniqueID();
124
		return $options;
125
	}
126
 
127
	/**
128
	 * Sets the text for the error message. Updates client-side erorr message.
129
	 * @param string the error message
130
	 */
131
	public function setErrorMessage($value)
132
	{
133
		parent::setErrorMessage($value);
134
		if($this->getActiveControl()->canUpdateClientSide())
135
		{
136
			$client = $this->getPage()->getCallbackClient();
137
			$func = 'Prado.Validation.setErrorMessage';
138
			$client->callClientFunction($func, array($this, $value));
139
		}
140
	}
141
 
142
		/**
143
	 * Ensure that the ID attribute is rendered and registers the javascript code
144
	 * for initializing the active control.
145
	 */
146
	protected function addAttributesToRender($writer)
147
	{
148
		parent::addAttributesToRender($writer);
149
		TBaseValidator::registerClientScriptValidator();
150
	}
151
 
152
	/**
153
	 * @return string corresponding javascript class name for this this.
154
	 */
155
	protected function getClientClassName()
156
	{
157
		return 'Prado.WebUI.TActiveCustomValidator';
158
	}
159
}
160
 
161
/**
162
 * Custom Validator callback client side options class.
163
 *
164
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
165
 * @version $Id: TActiveCustomValidator.php 2541 2008-10-21 15:05:13Z qiang.xue $
166
 * @package System.Web.UI.ActiveControls
167
 * @since 3.1
168
 */
169
class TActiveCustomValidatorClientSide extends TCallbackClientSide
170
{
171
	/**
172
	 * @return string javascript code for client-side OnValidate event.
173
	 */
174
	public function getOnValidate()
175
	{
176
		return $this->getOption('OnValidate');
177
	}
178
 
179
	/**
180
	 * Client-side OnValidate validator event is raise before the validators
181
	 * validation functions are called.
182
	 * @param string javascript code for client-side OnValidate event.
183
	 */
184
	public function setOnValidate($javascript)
185
	{
186
		$this->setFunction('OnValidate', $javascript);
187
	}
188
 
189
	/**
190
	 * Client-side OnSuccess event is raise after validation is successfull.
191
	 * This will override the default client-side validator behaviour.
192
	 * @param string javascript code for client-side OnSuccess event.
193
	 */
194
	public function setOnValidationSuccess($javascript)
195
	{
196
		$this->setFunction('OnValidationSuccess', $javascript);
197
	}
198
 
199
	/**
200
	 * @return string javascript code for client-side OnSuccess event.
201
	 */
202
	public function getOnValidationSuccess()
203
	{
204
		return $this->getOption('OnValidationSuccess');
205
	}
206
 
207
	/**
208
	 * Client-side OnError event is raised after validation failure.
209
	 * This will override the default client-side validator behaviour.
210
	 * @param string javascript code for client-side OnError event.
211
	 */
212
	public function setOnValidationError($javascript)
213
	{
214
		$this->setFunction('OnValidationError', $javascript);
215
	}
216
 
217
	/**
218
	 * @return string javascript code for client-side OnError event.
219
	 */
220
	public function getOnValidationError()
221
	{
222
		return $this->getOption('OnValidationError');
223
	}
224
 
225
	/**
226
	 * @param boolean true to revalidate when the control to validate changes value.
227
	 */
228
	public function setObserveChanges($value)
229
	{
230
		$this->setOption('ObserveChanges', TPropertyValue::ensureBoolean($value));
231
	}
232
 
233
	/**
234
	 * @return boolean true to observe changes.
235
	 */
236
	public function getObserveChanges()
237
	{
238
		$changes = $this->getOption('ObserveChanges');
239
		return is_null($changes) ? true : $changes;
240
	}
241
}