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
d8508cdf
Commit
d8508cdf
authored
Feb 12, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added docs about inline validation
fixes #2406
parent
db6d0a57
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
0 deletions
+36
-0
model.md
docs/guide/model.md
+36
-0
No files found.
docs/guide/model.md
View file @
d8508cdf
...
...
@@ -223,6 +223,42 @@ When `validate()` is called, the actual validation rules executed are determined
-
the rule must be active for the current scenario.
### Creating your own validators (Inline validators)
If none of the built in validators fit your needs, you can create your own validator by creating a method in you model class.
This method will be wrapped by an
[
[InlineValidator|yii\validation\InlineValidator
]
] an be called upon validation.
You will do the validation of the attribute and
[
[add errors|yii\base\Model::addError()
]
] to the model when validation fails.
The method has the following signature
`public function myValidator($attribute, $params)`
while you are free to choose the name.
Here is an example implementation of a validator validating the age of a user:
```
php
public
function
validateAge
(
$attribute
,
$params
)
{
$value
=
$this
->
$attribute
;
if
(
strtotime
(
$value
)
>
strtotime
(
'now - '
.
$params
[
'min'
]
.
' years'
))
{
$this
->
addError
(
$attribute
,
'You must be at least '
.
$params
[
'min'
]
.
' years old to register for this service.'
);
}
}
public
function
rules
()
{
return
[
// ...
[[
'birthdate'
],
'validateAge'
,
'params'
=>
[
'min'
=>
'12'
]],
];
}
```
You may also set other properties of the
[
[InlineValidator|yii\validation\InlineValidator
]
] in the rules definition,
for example the
[
[skipOnEmpty|yii\validation\InlineValidator::skipOnEmpty
]
] property:
```
php
[[
'birthdate'
],
'validateAge'
,
'params'
=>
[
'min'
=>
'12'
],
'skipOnEmpty'
=>
false
],
```
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