Yii2 modal popup activeform ajax form asynchronous validation

Source: Internet
Author: User
Tags yii
Author: White Wolf Source: http://www.manks.top/yii2_modal_activeform_ajax.html This article copyright belongs to the author, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location gives the original text connection, Otherwise, the right to pursue legal liability is retained.

Before we talked about how to use modal in yii2 and how to use modal in the Yii2 GridView list, I thought modal would be over to start a new topic, but the actual problem is often beyond imagination. This does not modal the window submitted by the table is how to verify the problem and come out again, come out again!

The essence of the problem is not modal, the core of the problem lies in the ActiveForm of the asynchronous verification, solve the first contradiction, we have solved the problem of this article. By the way, dinning a sentence, modal really have nothing to say. I'll change the words back if I have them back.

YII2, ActiveForm does client-side validation by default, but the form's submission is not without a refresh. That is, the page is refreshed when the form is often seen. If you want to turn on the no-refresh mode, just start enableajaxvalidation on ActiveForm, like this

 
  ' Form-id ',        ' enableajaxvalidation ' = True,        ' validationurl ' = Url::toroute ([' Validate-form ']),    ] );?>

Note that neither the ID nor the enableajaxvalidation can be less than one.

About Validateurl we make a note. If you do not set this parameter, the address is your current route by default, and it happens that your current route is the action of form form, you will be curious to find that when the form item input loses focus, your changes to the data have been submitted to the backend to be processed? This is often not what we want, at this time we need to set a routing address for validateurl, the meaning of the operation to be requested is to do the verification asynchronously! We look at the specific implementation:

Form submission operation, basically do not need to make changes 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//See the primary validation action, which is an asynchronous validation when the form field loses focus, and if the form is submitted directly, the action is also performed to verify 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, the Yii2 asynchronous no-refresh form verification is implemented simply!

The topic of this issue is short but the content is exquisite, hope to be helpful to you!

[Considering the current domestic web site most of the collection of articles very frequently, not to mention the original source, the original author would like to see the original text, in case there are any problems can not update all articles, to avoid misleading! ]

View Original

The above describes the Yii2 modal pop-up ActiveForm Ajax form asynchronous verification, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.