Asynchronous validation of ActiveForm ajax form in yii2 modal pop-up window, yii2activeform

Source: Internet
Author: User

Asynchronous validation of ActiveForm ajax form in yii2 modal pop-up window, yii2activeform

Author: White Wolf Source: http://www.manks.top/yii2_modal_activeform_ajax.html this article copyright to the author, welcome to reprint, but without the author's consent must retain this paragraph of the statement, and in the Article Page clearly given the original connection, otherwise, you are entitled to pursue legal liability.

The previous section describes how to use modal in yii2 and how to use modal in the update operation in the yii2 gridview list. I thought that modal would come to an end to start a new topic, but the actual problem is often beyond imagination. This is not the case that the form submitted by modal pop-up window says that the question of how to verify has come out again!

The essence of this problem is actually not very relevant to modal. The core of the problem lies in the asynchronous verification of ActiveForm, which solves the primary contradiction and solves the problem in this article. By the way, modal has nothing to say. If I had to change it back later.

In yii2, ActiveForm performs client verification by default, but the form submission is not refreshing. That is, after the forms are submitted, the page is refreshed. To enable the refreshing mode, you only need to enableAjaxValidation in ActiveForm, as shown below:

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

Note: Neither id nor enableAjaxValidation can be less.

Here is a description of validateUrl. If you do not set this parameter, the address is your current route by default, and it happens that your current route is the form action, you will be curious to find that, when the input of the form item loses focus, have you submitted the data modification to the backend for processing? This is often not what we want. In this case, we need to set a route address for validateUrl. The significance of the requested operation lies in asynchronous verification! Let's look at the specific implementation:

 

// Form submission operation. Basically, you do not need to modify if ($ model-> load (Yii: $ app-> request-> post ()) & $ model-> save () {return $ this-> redirect (['index']);} return $ this-> render ('create ', ['model' => $ model,]); // @ see the http://www.manks.top/yii2_modal_activeform_ajax.html// to see the main verification operation, which is asynchronous verification when the form field loses focus, and if the form is submitted directly, the 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 );}

In this way, yii2 asynchronous brushless New Form Verification is implemented!

This topic is short but has excellent content. I hope it will help you!

[Considering that most of the articles collected on Chinese websites are very frequent at present, the author does not specify the source of the original article. The original author prefers the readers to check the original article to avoid updating all the articles due to any problems and avoid misleading.]

View Original

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.