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
994780d1
Commit
994780d1
authored
Mar 23, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
guide about conditional validator
parent
4c939eee
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
0 deletions
+28
-0
model.md
docs/guide/model.md
+28
-0
No files found.
docs/guide/model.md
View file @
994780d1
...
@@ -258,6 +258,34 @@ for example the [[yii\validators\InlineValidator::$skipOnEmpty|skipOnEmpty]] pro
...
@@ -258,6 +258,34 @@ for example the [[yii\validators\InlineValidator::$skipOnEmpty|skipOnEmpty]] pro
[[
'birthdate'
],
'validateAge'
,
'params'
=>
[
'min'
=>
'12'
],
'skipOnEmpty'
=>
false
],
[[
'birthdate'
],
'validateAge'
,
'params'
=>
[
'min'
=>
'12'
],
'skipOnEmpty'
=>
false
],
```
```
### conditional validation
To validate attributes only when certain conditions apply, e.g. the validation of
one field depends on the value of another field you can use
[
[yii\validators\Validator::when|the `when` property
]
]
to define such conditions:
```
php
[
'state'
,
'required'
,
'when'
=>
function
(
$model
)
{
return
$model
->
country
==
Country
::
USA
;
}],
[
'stateOthers'
,
'required'
,
'when'
=>
function
(
$model
)
{
return
$model
->
country
!=
Country
::
USA
;
}],
[
'mother'
,
'required'
,
'when'
=>
function
(
$model
)
{
return
$model
->
age
<
18
&&
$model
->
married
!=
true
;
}],
```
For better readability the conditions can also be written like this:
```
php
public
function
rules
()
{
$usa
=
function
(
$model
)
{
return
$model
->
country
==
Country
::
USA
;
};
$notUsa
=
function
(
$model
)
{
return
$model
->
country
!=
Country
::
USA
;
};
$child
=
function
(
$model
)
{
return
$model
->
age
<
18
&&
$model
->
married
!=
true
;
};
return
[
[
'state'
,
'required'
,
'when'
=>
$usa
],
[
'stateOthers'
,
'required'
,
'when'
=>
$notUsa
],
// note that it is not possible to write !$usa
[
'mother'
,
'required'
,
'when'
=>
$child
],
];
}
```
Massive Attribute Retrieval and Assignment
Massive Attribute Retrieval and Assignment
------------------------------------------
------------------------------------------
...
...
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