Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
ee9f7ed4
Commit
ee9f7ed4
authored
Jun 20, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
doc improvement [skip ci]
parent
3acdd3ab
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
28 deletions
+48
-28
input-validation.md
docs/guide/input-validation.md
+48
-28
No files found.
docs/guide/input-validation.md
View file @
ee9f7ed4
...
@@ -67,6 +67,8 @@ of the following format:
...
@@ -67,6 +67,8 @@ of the following format:
// optional, specifies in which scenario(s) this rule should be applied
// optional, specifies in which scenario(s) this rule should be applied
// if not given, it means the rule applies to all scenarios
// if not given, it means the rule applies to all scenarios
// You may also configure the "except" option if you want to apply the rule
// to all scenarios except the listed ones
'on'
=>
[
'scenario1'
,
'scenario2'
,
...
],
'on'
=>
[
'scenario1'
,
'scenario2'
,
...
],
// optional, specifies additional configurations for the validator object
// optional, specifies additional configurations for the validator object
...
@@ -202,6 +204,39 @@ As you can see, these validation rules do not really validate the inputs. Instea
...
@@ -202,6 +204,39 @@ As you can see, these validation rules do not really validate the inputs. Instea
and save them back to the attributes being validated.
and save them back to the attributes being validated.
### Handling Empty Inputs <a name="handling-empty-inputs"></a>
When input data are submitted from HTML forms, you often need to assign some default values to the inputs
if they are empty. You can do so by using the
[
default
](
tutorial-core-validators.md#default
)
validator. For example,
```
php
[
// set "username" and "email" as null if they are empty
[[
'username'
,
'email'
],
'default'
],
// set "level" to be 1 if it is empty
[
'level'
,
'default'
,
'value'
=>
1
],
]
```
By default, an input is considered empty if its value is an empty string, an empty array or a null.
You may customize the default empty detection logic by configuring the the
[
[yii\validators\Validator::isEmpty
]
] property
with a PHP callable. For example,
```
php
[
[
'agree'
,
'required'
,
'isEmpty'
=>
function
(
$value
)
{
return
empty
(
$value
);
}],
]
```
> Note: Most validators do not handle empty inputs if their [[yii\base\Validator::skipOnEmpty]] property takes
the default value true. They will simply be skipped during validation if their associated attributes receive empty
inputs. Among the
[
core validators
](
tutorial-core-validators.md
)
, only the
`captcha`
,
`default`
,
`filter`
,
`required`
, and
`trim`
validators will handle empty inputs.
## Ad Hoc Validation <a name="ad-hoc-validation"></a>
## Ad Hoc Validation <a name="ad-hoc-validation"></a>
Sometimes you need to do
*ad hoc validation*
for values that are not bound to any model.
Sometimes you need to do
*ad hoc validation*
for values that are not bound to any model.
...
@@ -327,6 +362,16 @@ class MyForm extends Model
...
@@ -327,6 +362,16 @@ class MyForm extends Model
}
}
```
```
> Note: By default, inline validators will not be applied if their associated attributes receive empty inputs
or if they have already failed some validation rules. If you want to make sure a rule is always applied,
you may configure the
[
[yii\base\Validator::skipOnEmpty|skipOnEmpty
]
] and/or
[
[yii\base\Validator::skipOnError|skipOnError
]
]
properties to be false in the rule declarations. For example,
```
php
[
[
'country'
,
'validateCountry'
,
'skipOnEmpty'
=>
false
,
'skipOnError'
=>
false
],
]
```
### Standalone Validators <a name="standalone-validators"></a>
### Standalone Validators <a name="standalone-validators"></a>
...
@@ -357,33 +402,6 @@ instead of `validateAttribute()` and `validate()` because by default the latter
...
@@ -357,33 +402,6 @@ instead of `validateAttribute()` and `validate()` because by default the latter
by calling
`validateValue()`
.
by calling
`validateValue()`
.
### Handling Empty Inputs <a name="handling-empty-inputs"></a>
Validators often need to check if an input is empty or not. In your validator, you may call
[
[yii\validators\Validator::isEmpty()
]
]
to perform this check. By default, this method will return true if a value is an empty string, an empty array or null.
Users of validators can customize the default empty detection logic by configuring
the
[
[yii\validators\Validator::isEmpty
]
] property with a PHP callable. For example,
```
php
[
[
'agree'
,
'required'
,
'isEmpty'
=>
function
(
$value
)
{
return
empty
(
$value
);
}],
]
```
When input data are submitted from HTML forms, you often need to assign some default values to the inputs
if they are empty. You can do so by using the
[
default
](
tutorial-core-validators.md#default
)
validator. For example,
```
php
[
// set "level" to be 1 if it is empty
[
'level'
,
'default'
,
'value'
=>
1
],
]
```
## Client-Side Validation <a name="client-side-validation"></a>
## Client-Side Validation <a name="client-side-validation"></a>
Client-side validation based on JavaScript is desirable when end users provide inputs via HTML forms, because
Client-side validation based on JavaScript is desirable when end users provide inputs via HTML forms, because
...
@@ -453,7 +471,9 @@ Behind the scene, [[yii\widgets\ActiveForm]] will read the validation rules decl
...
@@ -453,7 +471,9 @@ Behind the scene, [[yii\widgets\ActiveForm]] will read the validation rules decl
and generate appropriate JavaScript code for validators that support client-side validation. When a user
and generate appropriate JavaScript code for validators that support client-side validation. When a user
changes the value of an input field or submit the form, the client-side validation JavaScript will be triggered.
changes the value of an input field or submit the form, the client-side validation JavaScript will be triggered.
If you do not want client-side validation, you may simply configure the
[
[yii\widgets\ActiveForm::enableClientValidation
]
]
If you want to turn off client-side validation completely, you may configure the
[
[yii\widgets\ActiveForm::enableClientValidation
]
] property to be false. You may also turn off client-side
validation of individual input fields by configuring their
[
[yii\widgets\ActiveField::enableClientValidation
]
]
property to be false.
property to be false.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment