Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TCustomValidator class file
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TCustomValidator.php 2551 2008-10-30 17:07:05Z carlgmathisen $
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
 * TCustomValidator class
20
 *
21
 * TCustomValidator performs user-defined validation (either
22
 * server-side or client-side or both) on an input component.
23
 *
24
 * To create a server-side validation function, provide a handler for
25
 * the {@link onServerValidate OnServerValidate} event that performs the validation.
26
 * The data string of the input control to validate can be accessed
27
 * by {@link TServerValidateEventParameter::getValue Value} of the event parameter.
28
 * The result of the validation should be stored in the
29
 * {@link TServerValidateEventParameter::getIsValid IsValid} property of the event
30
 * parameter.
31
 *
32
 * To create a client-side validation function, add the client-side
33
 * validation javascript function to the page template.
34
 * The function should have the following signature:
35
 * <code>
36
 * <script type="text/javascript"><!--
37
 * function ValidationFunctionName(sender, parameter)
38
 * {
39
 *    // if(parameter == ...)
40
 *    //    return true;
41
 *    // else
42
 *    //    return false;
43
 * }
44
 * --></script>
45
 * </code>
46
 * Use the {@link setClientValidationFunction ClientValidationFunction} property
47
 * to specify the name of the client-side validation script function associated
48
 * with the TCustomValidator.
49
 *
50
 * @author Qiang Xue <qiang.xue@gmail.com>
51
 * @version $Id: TCustomValidator.php 2551 2008-10-30 17:07:05Z carlgmathisen $
52
 * @package System.Web.UI.WebControls
53
 * @since 3.0
54
 */
55
class TCustomValidator extends TBaseValidator
56
{
57
	/**
58
	 * Gets the name of the javascript class responsible for performing validation for this control.
59
	 * This method overrides the parent implementation.
60
	 * @return string the javascript class name
61
	 */
62
	protected function getClientClassName()
63
	{
64
		return 'Prado.WebUI.TCustomValidator';
65
	}
66
 
67
	/**
68
	 * @return string the name of the custom client-side script function used for validation.
69
	 */
70
	public function getClientValidationFunction()
71
	{
72
		return $this->getViewState('ClientValidationFunction','');
73
	}
74
 
75
	/**
76
	 * Sets the name of the custom client-side script function used for validation.
77
	 * @param string the script function name
78
	 */
79
	public function setClientValidationFunction($value)
80
	{
81
		$this->setViewState('ClientValidationFunction',$value,'');
82
	}
83
 
84
	/**
85
	 * This method overrides the parent's implementation.
86
	 * The validation succeeds if {@link onServerValidate} returns true.
87
	 * @return boolean whether the validation succeeds
88
	 */
89
	public function evaluateIsValid()
90
	{
91
		$value = '';
92
		if($this->getValidationTarget()!==null)
93
			$value=$this->getValidationValue($this->getValidationTarget());
94
		return $this->onServerValidate($value);
95
	}
96
 
97
	/**
98
	 * This method is invoked when the server side validation happens.
99
	 * It will raise the <b>OnServerValidate</b> event.
100
	 * The method also allows derived classes to handle the event without attaching a delegate.
101
	 * <b>Note</b> The derived classes should call parent implementation
102
	 * to ensure the <b>OnServerValidate</b> event is raised.
103
	 * @param string the value to be validated
104
	 * @return boolean whether the value is valid
105
	 */
106
	public function onServerValidate($value)
107
	{
108
		$param=new TServerValidateEventParameter($value,true);
109
		$this->raiseEvent('OnServerValidate',$this,$param);
110
		if($this->getValidationTarget()==null)
111
			return true;
112
		else
113
			return $param->getIsValid();
114
	}
115
 
116
	/**
117
	 * @return TControl control to be validated. Null if no control is found.
118
	 */
119
	protected function getValidationTarget()
120
	{
121
		if(($id=$this->getControlToValidate())!=='' && ($control=$this->findControl($id))!==null)
122
			return $control;
123
		else if(($id=$this->getControlToValidate())!=='')
124
			throw new TInvalidDataTypeException('basevalidator_validatable_required',get_class($this));
125
		else
126
			return null;
127
	}
128
 
129
	/**
130
	 * Returns an array of javascript validator options.
131
	 * @return array javascript validator options.
132
	 */
133
	protected function getClientScriptOptions()
134
	{
135
		$options=parent::getClientScriptOptions();
136
		if(($clientJs=$this->getClientValidationFunction())!=='')
137
			$options['ClientValidationFunction']=$clientJs;
138
		return $options;
139
	}
140
 
141
	/**
142
	 * Only register the client-side validator if
143
	 * {@link setClientValidationFunction ClientValidationFunction} is set.
144
	 */
145
	protected function registerClientScriptValidator()
146
	{
147
		if($this->getClientValidationFunction()!=='')
148
			parent::registerClientScriptValidator();
149
	}
150
}
151
 
152
/**
153
 * TServerValidateEventParameter class
154
 *
155
 * TServerValidateEventParameter encapsulates the parameter data for
156
 * <b>OnServerValidate</b> event of TCustomValidator components.
157
 *
158
 * @author Qiang Xue <qiang.xue@gmail.com>
159
 * @version $Id: TCustomValidator.php 2551 2008-10-30 17:07:05Z carlgmathisen $
160
 * @package System.Web.UI.WebControls
161
 * @since 3.0
162
 */
163
class TServerValidateEventParameter extends TEventParameter
164
{
165
	/**
166
	 * the value to be validated
167
	 * @var string
168
	 */
169
	private $_value='';
170
	/**
171
	 * whether the value is valid
172
	 * @var boolean
173
	 */
174
	private $_isValid=true;
175
 
176
	/**
177
	 * Constructor.
178
	 * @param string property value to be validated
179
	 * @param boolean whether the value is valid
180
	 */
181
	public function __construct($value,$isValid)
182
	{
183
		$this->_value=$value;
184
		$this->setIsValid($isValid);
185
	}
186
 
187
	/**
188
	 * @return string value to be validated
189
	 */
190
	public function getValue()
191
	{
192
		return $this->_value;
193
	}
194
 
195
	/**
196
	 * @return boolean whether the value is valid
197
	 */
198
	public function getIsValid()
199
	{
200
		return $this->_isValid;
201
	}
202
 
203
	/**
204
	 * @param boolean whether the value is valid
205
	 */
206
	public function setIsValid($value)
207
	{
208
		$this->_isValid=TPropertyValue::ensureBoolean($value);
209
	}
210
}