forked from yiisoft/yii2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidator.php
268 lines (252 loc) · 9.56 KB
/
Validator.php
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
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\validators;
use Yii;
use yii\base\Component;
use yii\base\NotSupportedException;
/**
* Validator is the base class for all validators.
*
* Child classes should override the [[validateAttribute()]] method to provide the actual
* logic of performing data validation. Child classes may also override [[clientValidateAttribute()]]
* to provide client-side validation support.
*
* Validator declares a set of [[builtInValidators|built-in validators] which can
* be referenced using short names. They are listed as follows:
*
* - `boolean`: [[BooleanValidator]]
* - `captcha`: [[CaptchaValidator]]
* - `compare`: [[CompareValidator]]
* - `date`: [[DateValidator]]
* - `default`: [[DefaultValueValidator]]
* - `double`: [[NumberValidator]]
* - `email`: [[EmailValidator]]
* - `exist`: [[ExistValidator]]
* - `file`: [[FileValidator]]
* - `filter`: [[FilterValidator]]
* - `in`: [[RangeValidator]]
* - `integer`: [[NumberValidator]]
* - `match`: [[RegularExpressionValidator]]
* - `required`: [[RequiredValidator]]
* - `string`: [[StringValidator]]
* - `unique`: [[UniqueValidator]]
* - `url`: [[UrlValidator]]
*
* @author Qiang Xue <[email protected]>
* @since 2.0
*/
abstract class Validator extends Component
{
/**
* @var array list of built-in validators (name => class or configuration)
*/
public static $builtInValidators = array(
'boolean' => 'yii\validators\BooleanValidator',
'captcha' => 'yii\validators\CaptchaValidator',
'compare' => 'yii\validators\CompareValidator',
'date' => 'yii\validators\DateValidator',
'default' => 'yii\validators\DefaultValueValidator',
'double' => 'yii\validators\NumberValidator',
'email' => 'yii\validators\EmailValidator',
'exist' => 'yii\validators\ExistValidator',
'file' => 'yii\validators\FileValidator',
'filter' => 'yii\validators\FilterValidator',
'in' => 'yii\validators\RangeValidator',
'integer' => array(
'class' => 'yii\validators\NumberValidator',
'integerOnly' => true,
),
'match' => 'yii\validators\RegularExpressionValidator',
'number' => 'yii\validators\NumberValidator',
'required' => 'yii\validators\RequiredValidator',
'string' => 'yii\validators\StringValidator',
'unique' => 'yii\validators\UniqueValidator',
'url' => 'yii\validators\UrlValidator',
);
/**
* @var array list of attributes to be validated.
*/
public $attributes;
/**
* @var string the user-defined error message. It may contain the following placeholders which
* will be replaced accordingly by the validator:
*
* - `{attribute}`: the label of the attribute being validated
* - `{value}`: the value of the attribute being validated
*/
public $message;
/**
* @var array list of scenarios that the validator can be applied to.
*/
public $on = array();
/**
* @var array list of scenarios that the validator should not be applied to.
*/
public $except = array();
/**
* @var boolean whether this validation rule should be skipped if the attribute being validated
* already has some validation error according to some previous rules. Defaults to true.
*/
public $skipOnError = true;
/**
* @var boolean whether this validation rule should be skipped if the attribute value
* is null or an empty string.
*/
public $skipOnEmpty = true;
/**
* @var boolean whether to enable client-side validation for this validator.
* The actual client-side validation is done via the JavaScript code returned
* by [[clientValidateAttribute()]]. If that method returns null, even if this property
* is true, no client-side validation will be done by this validator.
*/
public $enableClientValidation = true;
/**
* Validates a single attribute.
* Child classes must implement this method to provide the actual validation logic.
* @param \yii\base\Model $object the data object to be validated
* @param string $attribute the name of the attribute to be validated.
*/
abstract public function validateAttribute($object, $attribute);
/**
* Creates a validator object.
* @param string $type the validator type. This can be a method name,
* a built-in validator name, a class name, or a path alias of the validator class.
* @param \yii\base\Model $object the data object to be validated.
* @param array|string $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 Validator the validator
*/
public static function createValidator($type, $object, $attributes, $params = array())
{
if (!is_array($attributes)) {
$attributes = preg_split('/[\s,]+/', $attributes, -1, PREG_SPLIT_NO_EMPTY);
}
$params['attributes'] = $attributes;
if (isset($params['on']) && !is_array($params['on'])) {
$params['on'] = preg_split('/[\s,]+/', $params['on'], -1, PREG_SPLIT_NO_EMPTY);
}
if (isset($params['except']) && !is_array($params['except'])) {
$params['except'] = preg_split('/[\s,]+/', $params['except'], -1, PREG_SPLIT_NO_EMPTY);
}
if (method_exists($object, $type)) {
// method-based validator
$params['class'] = __NAMESPACE__ . '\InlineValidator';
$params['method'] = $type;
} else {
if (isset(self::$builtInValidators[$type])) {
$type = self::$builtInValidators[$type];
}
if (is_array($type)) {
foreach ($type as $name => $value) {
$params[$name] = $value;
}
} else {
$params['class'] = $type;
}
}
return Yii::createObject($params);
}
/**
* Validates the specified object.
* @param \yii\base\Model $object the data object being validated
* @param array|null $attributes the list of attributes to be validated.
* Note that if an attribute is not associated with the validator,
* it will be ignored.
* If this parameter is null, every attribute listed in [[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) {
$skip = $this->skipOnError && $object->hasErrors($attribute)
|| $this->skipOnEmpty && $this->isEmpty($object->$attribute);
if (!$skip) {
$this->validateAttribute($object, $attribute);
}
}
}
/**
* Validates a value.
* A validator class can implement this method to support data validation out of the context of a data model.
* @param mixed $value the data value to be validated.
* @throws NotSupportedException if data validation without a model is not supported
*/
public function validateValue($value)
{
throw new NotSupportedException(get_class($this) . ' does not support validateValue().');
}
/**
* Returns the JavaScript needed for performing client-side validation.
*
* You may override this method to return the JavaScript validation code if
* the validator can support client-side validation.
*
* The following JavaScript variables are predefined and can be used in the validation code:
*
* - `attribute`: the name of the attribute being validated.
* - `value`: the value being validated.
* - `messages`: an array used to hold the validation error messages for the attribute.
*
* @param \yii\base\Model $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 \yii\web\ActiveForm::enableClientValidation
*/
public function clientValidateAttribute($object, $attribute)
{
return null;
}
/**
* Returns a value indicating whether the validator is active for the given scenario and attribute.
*
* A validator is active if
*
* - the validator's `on` property is empty, or
* - the validator's `on` property contains the specified scenario
*
* @param string $scenario scenario name
* @return boolean whether the validator applies to the specified scenario.
*/
public function isActive($scenario)
{
return !in_array($scenario, $this->except, true) && (empty($this->on) || in_array($scenario, $this->on, true));
}
/**
* Adds an error about the specified attribute to the model object.
* This is a helper method that performs message selection and internationalization.
* @param \yii\base\Model $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
*/
public function addError($object, $attribute, $message, $params = array())
{
$value = $object->$attribute;
$params['{attribute}'] = $object->getAttributeLabel($attribute);
$params['{value}'] = is_array($value) ? 'array()' : $value;
$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
*/
public function isEmpty($value, $trim = false)
{
return $value === null || $value === array() || $value === ''
|| $trim && is_scalar($value) && trim($value) === '';
}
}