Commit 1e9350ab by Carsten Brandt

guide on ajax validation

fixes #4482
parent 9e9175fe
...@@ -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. Its 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.
...@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment