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
1e9350ab
Commit
1e9350ab
authored
Jul 28, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
guide on ajax validation
fixes #4482
parent
9e9175fe
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
1 deletion
+27
-1
input-validation.md
docs/guide/input-validation.md
+26
-1
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
No files found.
docs/guide/input-validation.md
View file @
1e9350ab
...
@@ -408,7 +408,7 @@ Client-side validation based on JavaScript is desirable when end users provide i
...
@@ -408,7 +408,7 @@ Client-side validation based on JavaScript is desirable when end users provide i
it allows users to find out input errors faster and thus provides better user experience. You may use or implement
it allows users to find out input errors faster and thus provides better user experience. You may use or implement
a validator that supports client-side validation *in addition to* server-side validation.
a validator that supports client-side validation *in addition to* server-side validation.
> Info: While client-side validation is desirable, it is not a must. It
main purpose is to provider
users better
> Info: While client-side validation is desirable, it is not a must. It
s main purpose is to provide
users better
experience. Like input data coming from end users, you should never trust client-side validation. For this reason,
experience. Like input data coming from end users, you should never trust client-side validation. For this reason,
you should always perform server-side validation by calling [[yii\base\Model::validate()]], like
you should always perform server-side validation by calling [[yii\base\Model::validate()]], like
described in the previous subsections.
described in the previous subsections.
...
@@ -534,3 +534,28 @@ JS;
...
@@ -534,3 +534,28 @@ JS;
['status', 'in', 'range' => Status::find()->select('id')->asArray()->column()],
['status', 'in', 'range' => Status::find()->select('id')->asArray()->column()],
]
]
```
```
### Ajax validation
Some kind of validation can only be done on server side because only the server has the necessary information
for example validating uniqueness of user names or email addresses.
In this case you can use ajax based validation instead of client validation, which will trigger an ajax
request in the background to validate the input while keeping the same user experience as with client validation.
To enable ajax validation for the whole form, you have to set the
[
[yii\widgets\ActiveForm::enableAjaxValidation
]
] property to be
`true`
. You may also turn it on/off
for individual input fields by configuring their
[
[yii\widgets\ActiveField::enableAjaxValidation
]
]
property.
You also need to prepare the server so that it can handle the ajax request.
This can be achived by a code snippet like the following, which has to be put into your action:
```
php
if
(
Yii
::
$app
->
request
->
isAjax
&&
$model
->
load
(
Yii
::
$app
->
request
->
post
()))
{
Yii
::
$app
->
response
->
format
=
Response
::
FORMAT_JSON
;
return
ActiveForm
::
validate
(
$model
);
}
```
The above code will check whether the current request is an ajax request and if yes, it will answer
this request by running the validation and returning the errors in JSON format.
framework/CHANGELOG.md
View file @
1e9350ab
...
@@ -74,6 +74,7 @@ Yii Framework 2 Change Log
...
@@ -74,6 +74,7 @@ Yii Framework 2 Change Log
-
Bug #4427: Formatter could do one division too much (kmindi)
-
Bug #4427: Formatter could do one division too much (kmindi)
-
Bug #4453:
`yii message/extract`
wasn't properly writing to po files in case of multiple categories (samdark)
-
Bug #4453:
`yii message/extract`
wasn't properly writing to po files in case of multiple categories (samdark)
-
Bug #4469: Make
`Security::compareString()`
timing depend only on length of
`$actual`
input and add unit test. (tom--)
-
Bug #4469: Make
`Security::compareString()`
timing depend only on length of
`$actual`
input and add unit test. (tom--)
-
Bug #4470: Avoid endless loop when exporting logs with low values of flushInterval and eportInterval (cebe)
-
Bug: Fixed inconsistent return of
`\yii\console\Application::runAction()`
(samdark)
-
Bug: Fixed inconsistent return of
`\yii\console\Application::runAction()`
(samdark)
-
Bug: URL encoding for the route parameter added to
`\yii\web\UrlManager`
(klimov-paul)
-
Bug: URL encoding for the route parameter added to
`\yii\web\UrlManager`
(klimov-paul)
-
Bug: Fixed the bug that requesting protected or private action methods would cause 500 error instead of 404 (qiangxue)
-
Bug: Fixed the bug that requesting protected or private action methods would cause 500 error instead of 404 (qiangxue)
...
...
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