From 1e9350abe16ede1c380234b66186592958b7204f Mon Sep 17 00:00:00 2001 From: Carsten Brandt <mail@cebe.cc> Date: Mon, 28 Jul 2014 09:50:33 +0200 Subject: [PATCH] guide on ajax validation fixes #4482 --- docs/guide/input-validation.md | 27 ++++++++++++++++++++++++++- framework/CHANGELOG.md | 1 + 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/guide/input-validation.md b/docs/guide/input-validation.md index bfa6b7c..8acfb6e 100644 --- a/docs/guide/input-validation.md +++ b/docs/guide/input-validation.md @@ -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 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, you should always perform server-side validation by calling [[yii\base\Model::validate()]], like described in the previous subsections. @@ -534,3 +534,28 @@ JS; ['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. diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index d7718b3..c6a0339 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -74,6 +74,7 @@ Yii Framework 2 Change Log - 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 #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: 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) -- libgit2 0.27.1