Yii2 modal window activeform ajax form Validation _php instance

Source: Internet
Author: User
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!

First of all, aside from modal, we will yii2 ActiveForm how to submit a form in an AJAX way to do a simple explanation, which is the focus of our topic today, 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

<?php $form = Activeform::begin ([' id ' = ' form-id ', ' enableajaxvalidation ' = = True,]);?>

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

Then look at the implementation of the service side

if ($model->load (yii:: $app->request->post ())) {yii:: $app->response->format = yii\web\response:: Format_json;if ($errors = \yii\widgets\activeform::validate ($model)) {return $errors;} else {if ($model->save (False ) {return $this->redirect ([' Index ']);}}} return $this->render (' Create ', [' model ' = $model,]);

This is a simple implementation of the YII2 asynchronous no flush submission form!

In fact, the following say and do not say that it is not important, mainly for some lazy reference it. Smart people who read the headline should understand how to solve the problem of modal submitting a form through ActiveForm.

In order to be compatible with modal, note that we are talking about compatibility rather than implementation, we have made a slight change to the program, only for reference.

if ($model->load (yii:: $app->request->post ())) {if ($model->save ()) {if (Yii:: $app->request-> Isajax) {Yii:: $app->response->format = \yii\web\response::format_json;return [' success ' = ' = True];} return $this->redirect ([' Index ']);} else {if (Yii:: $app->request->isajax) {yii:: $app->response->format = \yii\web\response::format_json; Return \yii\widgets\activeform::validate ($model);}} if (Yii:: $app->request->isajax) {return $this->renderajax (' Create ', [' model ' = $model,]);} else {return $ This->render (' Create ', [' model ' = $model,]);

The above is a small series to introduce you to the Yii2 modal window activeform ajax form validation related knowledge, I hope to help you, if you want to learn more about the script home site!

  • 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.