|
1 | 1 | Validating Input
|
2 | 2 | ================
|
3 | 3 |
|
| 4 | +> Note: This section is under development. |
| 5 | +
|
4 | 6 | In the [Models](structure-models.md#validation) section, we have described how data validation works
|
5 | 7 | in general. In this section, we will mainly focus on describing core validators, how to define your
|
6 | 8 | own validators, and different ways of using validators.
|
7 | 9 |
|
8 | 10 |
|
9 |
| -## Error Messages |
10 |
| - |
11 |
| -## Core Validators |
12 |
| - |
13 |
| -Yii provides a set of commonly used validators, found primarily within the `yii\validators` namespace. |
14 |
| - |
15 |
| -Instead of using lengthy validator class names, you may use *aliases* to specify the use of these core |
16 |
| -validators. For example, you can use the alias `required` to refer to the [[yii\validators\RequiredValidator]] class: |
17 |
| - |
18 |
| -```php |
19 |
| -public function rules() |
20 |
| -{ |
21 |
| - return [ |
22 |
| - [['email', 'password'], 'required'], |
23 |
| - ]; |
24 |
| -} |
25 |
| -``` |
26 |
| - |
27 |
| -The [[yii\validators\Validator::builtInValidators]] property declares all supported validator aliases. |
28 |
| - |
29 |
| -In the following, we will describe the main usage and properties of every core validator. |
30 |
| - |
31 |
| - |
32 |
| -### [[yii\validators\BooleanValidator|boolean]] <a name="boolean"></a> |
33 |
| - |
34 |
| -This validator checks if the input value is a boolean. |
35 |
| - |
36 |
| -- `trueValue`: the value representing *true*. Defaults to `'1'`. |
37 |
| -- `falseValue`: the value representing *false*. Defaults to `'0'`. |
38 |
| -- `strict`: whether the type of the input value should match that of `trueValue` and `falseValue`. Defaults to `false`. |
39 |
| - |
40 |
| -```php |
41 |
| -[ |
42 |
| - // checks if "selected" is either 0 or 1, regardless of data type |
43 |
| - ['selected', 'boolean'], |
44 |
| - |
45 |
| - // checks if "deleted" is of boolean type, either true or false |
46 |
| - ['deleted', 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => true], |
47 |
| -] |
48 |
| -``` |
49 |
| - |
50 |
| -> Note: Because data input submitted via HTML forms are all strings, you normally should leave the |
51 |
| - [[yii\validators\BooleanValidator::strict|strict]] property as false. |
52 |
| - |
53 |
| - |
54 |
| -### [[yii\captcha\CaptchaValidator|captcha]] <a name="captcha"></a> |
55 |
| - |
56 |
| -This validator is usually used together with [[yii\captcha\CaptchaAction]] and [[yii\captcha\Captcha]] |
57 |
| -to make sure an input is the same as the verification code displayed by [[yii\captcha\Captcha|CAPTCHA]] widget. |
58 |
| - |
59 |
| -- `caseSensitive`: whether the comparison of the verification code is case sensitive. Defaults to false. |
60 |
| -- `captchaAction`: the [route](structure-controllers.md#routes) corresponding to the |
61 |
| - [[yii\captcha\CaptchaAction|CAPTCHA action]] that renders the CAPTCHA image. Defaults to `'site/captcha'`. |
62 |
| -- `skipOnEmpty`: whether the validation can be skipped if the input is empty. Defaults to false, |
63 |
| - which means the input is required. |
64 |
| - |
65 |
| -```php |
66 |
| -[ |
67 |
| - ['verificationCode', 'captcha'], |
68 |
| -] |
69 |
| -``` |
70 |
| - |
71 |
| - |
72 |
| -### [[yii\validators\CompareValidator|compare]] <a name="compare"></a> |
73 |
| - |
74 |
| -This validator compares the specified input value with another one and make sure if their relationship |
75 |
| -is as specified by the `operator` property. |
76 |
| - |
77 |
| -- `compareAttribute`: the name of the attribute whose value should be compared with. When the validator |
78 |
| - is being used to validate an attribute, the default value of this property would be the name of |
79 |
| - the attribute suffixed with `_repeat`. For example, if the attribute being validated is `password`, |
80 |
| - then this property will default to `password_repeat`. |
81 |
| -- `compareValue`: a constant value that the input value should be compared with. When both |
82 |
| - of this property and `compareAttribute` are specified, this property will take precedence. |
83 |
| -- `operator`: the comparison operator. Defaults to `==`, meaning checking if the input value is equal |
84 |
| - to that of `compareAttribute` or `compareValue`. The following operators are supported: |
85 |
| - * `==`: check if two values are equal. The comparison is done is non-strict mode. |
86 |
| - * `===`: check if two values are equal. The comparison is done is strict mode. |
87 |
| - * `!=`: check if two values are NOT equal. The comparison is done is non-strict mode. |
88 |
| - * `!==`: check if two values are NOT equal. The comparison is done is strict mode. |
89 |
| - * `>`: check if value being validated is greater than the value being compared with. |
90 |
| - * `>=`: check if value being validated is greater than or equal to the value being compared with. |
91 |
| - * `<`: check if value being validated is less than the value being compared with. |
92 |
| - * `<=`: check if value being validated is less than or equal to the value being compared with. |
93 |
| - |
94 |
| -```php |
95 |
| -[ |
96 |
| - // validates if the value of "password" attribute equals to that of "password_repeat" |
97 |
| - ['password', 'compare'], |
98 |
| - |
99 |
| - // validates if age is greater than or equal to 30 |
100 |
| - ['age', 'compare', 'compareValue' => 30, 'operator' => '>='], |
101 |
| -] |
102 |
| -``` |
103 |
| - |
104 |
| - |
105 |
| -### [[yii\validators\DateValidator|date]] <a name="date"></a> |
106 |
| - |
107 |
| -Verifies if the attribute represents a date, time, or datetime in a proper format. |
108 |
| - |
109 |
| -- `format`, the date format that the value being validated should follow according to |
110 |
| - [PHP date_create_from_format](http://www.php.net/manual/en/datetime.createfromformat.php). _('Y-m-d')_ |
111 |
| -- `timestampAttribute`, the name of the attribute that should receive the parsed result. |
112 |
| - |
113 |
| -### [[yii\validators\DefaultValueValidator|default]] <a name="default"></a> |
114 |
| - |
115 |
| -Sets the attribute to be the specified default value. |
116 |
| - |
117 |
| -- `value`, the default value to be assigned. |
118 |
| - |
119 |
| -### [[yii\validators\NumberValidator|double]] <a name="double"></a> |
120 |
| - |
121 |
| -Validates that the attribute value is a number, integer or decimal. |
122 |
| - |
123 |
| -- `max`, the upper limit of the number (inclusive). _(null)_ |
124 |
| -- `min`, the lower limit of the number (inclusive). _(null)_ |
125 |
| - |
126 |
| -### [[yii\validators\EmailValidator|email]] <a name="email"></a> |
| 11 | +## Declaring Validation Rules |
127 | 12 |
|
128 |
| -Validates that the attribute value is a valid email address. By default, this validator checks if the attribute value is a syntactical valid email address, but the validator can be configured to check the address's domain for the address's existence. |
| 13 | +## Data Validation |
129 | 14 |
|
130 |
| -- `allowName`, whether to allow the name in the email address (e.g. `John Smith <[email protected]>`). _(false) _. |
131 |
| -- `checkMX`, whether to check the MX record for the email address. _(false)_ |
132 |
| -- `checkPort`, whether to check port 25 for the email address. _(false)_ |
133 |
| -- `enableIDN`, whether the validation process should take into account IDN (internationalized domain names). _(false)_ |
| 15 | +### Getting Error Messages |
134 | 16 |
|
135 |
| -### [[yii\validators\ExistValidator|exist]] <a name="exist"></a> |
| 17 | +### Empty Values |
136 | 18 |
|
137 |
| -Validates that the attribute value exists in a table. |
| 19 | +### Array Values |
138 | 20 |
|
139 |
| -- `targetClass`, the ActiveRecord class name or alias of the class that should be used to look for the attribute value being |
140 |
| - validated. _(ActiveRecord class of the attribute being validated)_ |
141 |
| -- `targetAttribute`, the ActiveRecord attribute name that should be used to look for the attribute value being validated. |
142 |
| - _(name of the attribute being validated)_ |
143 |
| - |
144 |
| -### [[yii\validators\FileValidator|file]] <a name="file"></a> |
145 |
| - |
146 |
| -Verifies if an attribute is receiving a valid uploaded file. |
147 |
| - |
148 |
| -- `types`, an array of file name extensions that are allowed to be uploaded. _(any)_ |
149 |
| -- `minSize`, the minimum number of bytes required for the uploaded file. |
150 |
| -- `maxSize`, the maximum number of bytes allowed for the uploaded file. |
151 |
| -- `maxFiles`, the maximum number of files that the given attribute can hold. _(1)_ |
152 |
| - |
153 |
| -### [[yii\validators\FilterValidator|filter]] <a name="filter"></a> |
154 |
| - |
155 |
| -Converts the attribute value by sending it through a filter. |
156 |
| - |
157 |
| -- `filter`, a PHP callback that defines a filter. |
158 |
| - |
159 |
| -Typically a callback is either the name of PHP function: |
160 |
| - |
161 |
| -```php |
162 |
| -['password', 'filter', 'filter' => 'trim'], |
163 |
| -``` |
164 |
| - |
165 |
| -Or an anonymous function: |
166 |
| - |
167 |
| -```php |
168 |
| -['text', 'filter', 'filter' => function ($value) { |
169 |
| - // here we are removing all swear words from text |
170 |
| - return $newValue; |
171 |
| -}], |
172 |
| -``` |
173 |
| - |
174 |
| -### [[yii\validators\ImageValidator|image]] <a name="image"></a> |
175 |
| - |
176 |
| -### [[yii\validators\RangeValidator|in]] <a name="in"></a> |
177 |
| - |
178 |
| -Validates that the attribute value is among a list of values. |
179 |
| - |
180 |
| -- `range`, a list of valid values that the attribute value should be among (inclusive). |
181 |
| -- `strict`, whether the comparison should be strict (both the type and value must be the same). _(false)_ |
182 |
| -- `not`, whether to invert the validation logic. _(false)_ |
183 |
| - |
184 |
| - |
185 |
| -### [[yii\validators\NumberValidator|integer]] <a name="integer"></a> |
186 |
| - |
187 |
| -Validates that the attribute value is an integer. |
188 |
| - |
189 |
| -- `max`, the upper limit of the number (inclusive). _(null)_ |
190 |
| -- `min`, the lower limit of the number (inclusive). _(null)_ |
191 |
| - |
192 |
| -### [[yii\validators\RegularExpressionValidator|match]] <a name="match"></a> |
193 |
| - |
194 |
| -Validates that the attribute value matches the specified pattern defined by a regular expression. |
195 |
| - |
196 |
| -- `pattern`, the regular expression to be matched. |
197 |
| -- `not`, whether to invert the validation logic. _(false)_ |
198 |
| - |
199 |
| -### [[yii\validators\NumberValidator|number]] <a name="number"></a> |
200 |
| - |
201 |
| -Validates that the attribute value is a number. |
202 |
| - |
203 |
| -- `max`, the upper limit of the number (inclusive). _(null)_ |
204 |
| -- `min`, the lower limit of the number (inclusive). _(null)_ |
205 |
| - |
206 |
| -### [[yii\validators\RequiredValidator|required]] <a name="required"></a> |
207 |
| - |
208 |
| -Validates that the specified attribute does not have a null or empty value. |
209 |
| - |
210 |
| -- `requiredValue`, the desired value that the attribute must have. _(any)_ |
211 |
| -- `strict`, whether the comparison between the attribute value and |
212 |
| - [[yii\validators\RequiredValidator::requiredValue|requiredValue]] must match both value and type. _(false)_ |
213 |
| - |
214 |
| -### [[yii\validators\SafeValidator|safe]] <a name="safe"></a> |
215 |
| - |
216 |
| -Serves as a dummy validator whose main purpose is to mark the attributes to be safe for massive assignment. |
217 |
| - |
218 |
| -### [[yii\validators\StringValidator|string]] <a name="string"></a> |
219 |
| - |
220 |
| -Validates that the attribute value is of certain length. |
221 |
| - |
222 |
| -- `length`, specifies the length limit of the value to be validated (inclusive). Can be `exactly X`, `[min X]`, `[min X, max Y]`. |
223 |
| -- `max`, the upper length limit (inclusive). If not set, it means no maximum length limit. |
224 |
| -- `min`, the lower length limit (inclusive). If not set, it means no minimum length limit. |
225 |
| -- `encoding`, the encoding of the string value to be validated. _([[yii\base\Application::charset]])_ |
226 |
| - |
227 |
| -### [[yii\validators\FilterValidator|trim]] <a name="trim"></a> |
228 |
| - |
229 |
| -### [[yii\validators\UniqueValidator|unique]] <a name="unique"></a> |
230 |
| - |
231 |
| -Validates that the attribute value is unique in the corresponding database table. |
232 |
| - |
233 |
| -- `targetClass`, the ActiveRecord class name or alias of the class that should be used to look for the attribute value being |
234 |
| - validated. _(ActiveRecord class of the attribute being validated)_ |
235 |
| -- `targetAttribute`, the ActiveRecord attribute name that should be used to look for the attribute value being validated. |
236 |
| - _(name of the attribute being validated)_ |
237 |
| - |
238 |
| -### [[yii\validators\UrlValidator|url]] <a name="url"></a> |
239 |
| - |
240 |
| -Validates that the attribute value is a valid http or https URL. |
| 21 | +## Data Filtering |
241 | 22 |
|
242 |
| -- `validSchemes`, an array of URI schemes that should be considered valid. _['http', 'https']_ |
243 |
| -- `defaultScheme`, the default URI scheme. If the input doesn't contain the scheme part, the default scheme will be |
244 |
| - prepended to it. _(null)_ |
245 |
| -- `enableIDN`, whether the validation process should take into account IDN (internationalized domain names). _(false)_ |
246 | 23 |
|
247 | 24 |
|
248 | 25 | ## Creating Validators
|
@@ -336,7 +113,7 @@ public function rules()
|
336 | 113 | This guide describes all of Yii's validators and their parameters.
|
337 | 114 |
|
338 | 115 |
|
339 |
| -## Data Filtering |
| 116 | + |
340 | 117 |
|
341 | 118 | ## Ad Hoc Validation
|
342 | 119 |
|
|
0 commit comments