CValidator.php
9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* CValidator class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CValidator is the base class for all validators.
*
* Child classes must implement the {@link validateAttribute} method.
*
* The following properties are defined in CValidator:
* <ul>
* <li>{@link attributes}: array, list of attributes to be validated;</li>
* <li>{@link message}: string, the customized error message. The message
* may contain placeholders that will be replaced with the actual content.
* For example, the "{attribute}" placeholder will be replaced with the label
* of the problematic attribute. Different validators may define additional
* placeholders.</li>
* <li>{@link on}: string, in which scenario should the validator be in effect.
* This is used to match the 'on' parameter supplied when calling {@link CModel::validate}.</li>
* </ul>
*
* When using {@link createValidator} to create a validator, the following aliases
* are recognized as the corresponding built-in validator classes:
* <ul>
* <li>required: {@link CRequiredValidator}</li>
* <li>filter: {@link CFilterValidator}</li>
* <li>match: {@link CRegularExpressionValidator}</li>
* <li>email: {@link CEmailValidator}</li>
* <li>url: {@link CUrlValidator}</li>
* <li>unique: {@link CUniqueValidator}</li>
* <li>compare: {@link CCompareValidator}</li>
* <li>length: {@link CStringValidator}</li>
* <li>in: {@link CRangeValidator}</li>
* <li>numerical: {@link CNumberValidator}</li>
* <li>captcha: {@link CCaptchaValidator}</li>
* <li>type: {@link CTypeValidator}</li>
* <li>file: {@link CFileValidator}</li>
* <li>default: {@link CDefaultValueValidator}</li>
* <li>exist: {@link CExistValidator}</li>
* <li>boolean: {@link CBooleanValidator}</li>
* <li>date: {@link CDateValidator}</li>
* <li>safe: {@link CSafeValidator}</li>
* <li>unsafe: {@link CUnsafeValidator}</li>
* </ul>
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.validators
* @since 1.0
*/
abstract class CValidator extends CComponent
{
/**
* @var array list of built-in validators (name=>class)
*/
public static $builtInValidators=array(
'required'=>'CRequiredValidator',
'filter'=>'CFilterValidator',
'match'=>'CRegularExpressionValidator',
'email'=>'CEmailValidator',
'url'=>'CUrlValidator',
'unique'=>'CUniqueValidator',
'compare'=>'CCompareValidator',
'length'=>'CStringValidator',
'in'=>'CRangeValidator',
'numerical'=>'CNumberValidator',
'captcha'=>'CCaptchaValidator',
'type'=>'CTypeValidator',
'file'=>'CFileValidator',
'default'=>'CDefaultValueValidator',
'exist'=>'CExistValidator',
'boolean'=>'CBooleanValidator',
'safe'=>'CSafeValidator',
'unsafe'=>'CUnsafeValidator',
'date'=>'CDateValidator',
);
/**
* @var array list of attributes to be validated.
*/
public $attributes;
/**
* @var string the user-defined error message. Different validators may define various
* placeholders in the message that are to be replaced with actual values. All validators
* recognize "{attribute}" placeholder, which will be replaced with the label of the attribute.
*/
public $message;
/**
* @var boolean whether this validation rule should be skipped when there is already a validation
* error for the current attribute. Defaults to false.
* @since 1.1.1
*/
public $skipOnError=false;
/**
* @var array list of scenarios that the validator should be applied.
* Each array value refers to a scenario name with the same name as its array key.
*/
public $on;
/**
* @var array list of scenarios that the validator should not be applied to.
* Each array value refers to a scenario name with the same name as its array key.
* @since 1.1.11
*/
public $except;
/**
* @var boolean whether attributes listed with this validator should be considered safe for massive assignment.
* Defaults to true.
* @since 1.1.4
*/
public $safe=true;
/**
* @var boolean whether to perform client-side validation. Defaults to true.
* Please refer to {@link CActiveForm::enableClientValidation} for more details about client-side validation.
* @since 1.1.7
*/
public $enableClientValidation=true;
/**
* Validates a single attribute.
* This method should be overridden by child classes.
* @param CModel $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
*/
abstract protected function validateAttribute($object,$attribute);
/**
* Creates a validator object.
* @param string $name the name or class of the validator
* @param CModel $object the data object being validated that may contain the inline validation method
* @param mixed $attributes list of attributes to be validated. This can be either an array of
* the attribute names or a string of comma-separated attribute names.
* @param array $params initial values to be applied to the validator properties
* @return CValidator the validator
*/
public static function createValidator($name,$object,$attributes,$params=array())
{
if(is_string($attributes))
$attributes=preg_split('/[\s,]+/',$attributes,-1,PREG_SPLIT_NO_EMPTY);
if(isset($params['on']))
{
if(is_array($params['on']))
$on=$params['on'];
else
$on=preg_split('/[\s,]+/',$params['on'],-1,PREG_SPLIT_NO_EMPTY);
}
else
$on=array();
if(isset($params['except']))
{
if(is_array($params['except']))
$except=$params['except'];
else
$except=preg_split('/[\s,]+/',$params['except'],-1,PREG_SPLIT_NO_EMPTY);
}
else
$except=array();
if(method_exists($object,$name))
{
$validator=new CInlineValidator;
$validator->attributes=$attributes;
$validator->method=$name;
if(isset($params['clientValidate']))
{
$validator->clientValidate=$params['clientValidate'];
unset($params['clientValidate']);
}
$validator->params=$params;
if(isset($params['skipOnError']))
$validator->skipOnError=$params['skipOnError'];
}
else
{
$params['attributes']=$attributes;
if(isset(self::$builtInValidators[$name]))
$className=Yii::import(self::$builtInValidators[$name],true);
else
$className=Yii::import($name,true);
$validator=new $className;
foreach($params as $name=>$value)
$validator->$name=$value;
}
$validator->on=empty($on) ? array() : array_combine($on,$on);
$validator->except=empty($except) ? array() : array_combine($except,$except);
return $validator;
}
/**
* Validates the specified object.
* @param CModel $object the data object being validated
* @param array $attributes the list of attributes to be validated. Defaults to null,
* meaning every attribute listed in {@link attributes} will be validated.
*/
public function validate($object,$attributes=null)
{
if(is_array($attributes))
$attributes=array_intersect($this->attributes,$attributes);
else
$attributes=$this->attributes;
foreach($attributes as $attribute)
{
if(!$this->skipOnError || !$object->hasErrors($attribute))
$this->validateAttribute($object,$attribute);
}
}
/**
* Returns the JavaScript needed for performing client-side validation.
* Do not override this method if the validator does not support client-side validation.
* Two predefined JavaScript variables can be used:
* <ul>
* <li>value: the value to be validated</li>
* <li>messages: an array used to hold the validation error messages for the value</li>
* </ul>
* @param CModel $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. Null if the validator does not support client-side validation.
* @see CActiveForm::enableClientValidation
* @since 1.1.7
*/
public function clientValidateAttribute($object,$attribute)
{
}
/**
* Returns a value indicating whether the validator applies to the specified scenario.
* A validator applies to a scenario as long as any of the following conditions is met:
* <ul>
* <li>the validator's "on" property is empty</li>
* <li>the validator's "on" property contains the specified scenario</li>
* </ul>
* @param string $scenario scenario name
* @return boolean whether the validator applies to the specified scenario.
*/
public function applyTo($scenario)
{
if(isset($this->except[$scenario]))
return false;
return empty($this->on) || isset($this->on[$scenario]);
}
/**
* Adds an error about the specified attribute to the active record.
* This is a helper method that performs message selection and internationalization.
* @param CModel $object the data object being validated
* @param string $attribute the attribute being validated
* @param string $message the error message
* @param array $params values for the placeholders in the error message
*/
protected function addError($object,$attribute,$message,$params=array())
{
$params['{attribute}']=$object->getAttributeLabel($attribute);
$object->addError($attribute,strtr($message,$params));
}
/**
* Checks if the given value is empty.
* A value is considered empty if it is null, an empty array, or the trimmed result is an empty string.
* Note that this method is different from PHP empty(). It will return false when the value is 0.
* @param mixed $value the value to be checked
* @param boolean $trim whether to perform trimming before checking if the string is empty. Defaults to false.
* @return boolean whether the value is empty
*/
protected function isEmpty($value,$trim=false)
{
return $value===null || $value===array() || $value==='' || $trim && is_scalar($value) && trim($value)==='';
}
}