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
10eedae1
Commit
10eedae1
authored
Jul 29, 2014
by
Alex-Code
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update input-validation.md
parent
66e0e8aa
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
0 deletions
+51
-0
input-validation.md
docs/guide/input-validation.md
+51
-0
No files found.
docs/guide/input-validation.md
View file @
10eedae1
...
...
@@ -487,6 +487,7 @@ predefined variables:
- `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.
- `deferred`: an array which deferred objects can be pushed into.
In the following example, we create a `StatusValidator` which validates if an input is a valid status input
against the existing status data. The validator supports both server side and client side validation.
...
...
@@ -535,6 +536,56 @@ JS;
]
```
### Deferred validation
If you need to perform any asynchronous validation you can use a
[
deferred object
](
http://api.jquery.com/category/deferred-object/
)
.
deferred objects must be pushed to the
```deferred```
array for validation to use them.
Once any asynchronous validation has finished you must call
```resolve()```
on the Deferred object for it to complete.
This example shows reading an image to check the dimensions client side (
```file```
will be from an input of type=file).
```
php
...
public
function
clientValidateAttribute
(
$model
,
$attribute
,
$view
)
{
return
<<<JS
var def = $.Deferred();
var img = new Image();
img.onload = function() {
if (this.width > 150) {
messages.push('Image too wide!!');
}
def.resolve();
}
var reader = new FileReader();
reader.onloadend = function() {
img.src = reader.result;
}
reader.readAsDataURL(file);
deferred.push(def);
JS;
}
...
```
Ajax can also be used and pushed straight into the deferred array.
```
deferred.push($.get("/check", {value: value}).done(function(data) {
if ('' !== data) {
messages.push(data);
}
}));
```
The
```deferred```
array also has a shortcut method
```add```
.
```
deferred.add(function() {
//Asynchronous Validation here
//The context of this function is the Deferred object where resolve can be called.
});
```
### Ajax validation
Some kind of validation can only be done on server side because only the server has the necessary information
...
...
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