ActiveForm ajax form asynchronous validation of yii2 modal pop-up window

WBOY
Release: 2016-07-28 08:29:53
Original
1144 people have browsed it

Author: Bailang Source: http://www.manks.top/yii2_modal_activeform_ajax.html The copyright of this article belongs to the author, and you are welcome to reprint it. However, this statement must be retained without the author’s consent, and a link to the original text must be provided in an obvious position on the article page. Otherwise, we reserve the right to pursue legal liability.

Previously we talked about how to use modal in yii2 and how to use modal in the update operation in yii2 gridview list. I thought that modal would come to an end and we could start a new topic, but the actual problems are often beyond imagination. This is not the modal pop-up window. The submitted form said that the question of how to verify it came up again, it came out again!

The essence of this problem actually has little to do with modal. The core of the problem lies in the asynchronous verification of ActiveForm, which solves the primary contradiction and solves the problem of our article. By the way, modal really has nothing to say. I'll change it back later if I have to.

In yii2, ActiveForm does client-side verification by default, but form submission is not refresh-free. That is to say, it is often seen that the page will refresh after the form is submitted. If you want to enable the refresh-free mode, you only need to enable enableAjaxValidation in ActiveForm, as shown below

<?php $form = ActiveForm::begin([
        'id' => 'form-id',
        'enableAjaxValidation' => true,
        'validationUrl' => Url::toRoute(['validate-form']),
    ]
); ?>
Copy after login

Note that neither id nor enableAjaxValidation can be missing.

Let’s make an explanation about validateUrl. If you do not set this parameter, the address will default to your current route, and your current route happens to be the action of the form. You will be curious to find that when the form input loses focus, your modifications to the data have been submitted. Has it been processed at the back end? This is often not what we want. At this time, we need to set a routing address for validateUrl. The meaning of the requested operation is to perform asynchronous verification! Let’s look at the specific implementation:

//表单提交操作,基本上不需要做改动
if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['index']);
    }
}
return $this->render('create', [
    'model' => $model,
]);

// @see http://www.manks.top/yii2_modal_activeform_ajax.html
// 看主要的验证操作,该操作是表单字段失去焦点时异步验证,同时如果直接提交表单,也会先执行该操作进行验证
public function actionValidateForm () {
    Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    $model = new Model();  
    $model->load(Yii::$app->request->post());  
    return \yii\widgets\ActiveForm::validate($model);  
}
Copy after login

In this way, yii2 asynchronous non-refresh form verification is simply implemented!

The theme of this issue is short but has superb content. I hope it will be helpful to you!

[Considering that most domestic websites currently collect articles very frequently, and some even do not indicate the source of the original article, the original author hopes that readers can check the original article to prevent any problems and not update all articles to avoid misleading! ]

View original text

The above has introduced the ActiveForm ajax form asynchronous verification of yii2 modal pop-up window, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!