Yii2 modal window ActiveForm Ajax form asynchronous validation _php instance

Source: Internet
Author: User
Tags yii

We talked about how to use the modal in the Yii2 and how to use modal in Yii2 in the GridView list, and we thought modal would come to an ending to start a new topic, but the actual problem was often beyond imagination, This does not modal the window to submit the table to say how to verify the problem again came out, and came out!

First of all, regardless of modal, we will yii2 ActiveForm how to submit the form in an AJAX way to do a simple explanation, which is also the focus of our today's topic, modal really have nothing to say. I'll change the words back if there is.

In Yii2, ActiveForm defaults to client-side validation, but the form's submission is not flush-free. That is, the page is refreshed when you see a form submission. If you want to open the mode without refreshing, just start the enableajaxvalidation on the ActiveForm, like the following

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

Note Oh, ID and enableajaxvalidation can not 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,
]);

So it is simple to implement the Yii2 asynchronous no refresh submission form!

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

In order to be compatible with modal, note that we are talking about compatibility rather than implementation, we have slightly changed the program, just 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 set up to introduce the Yii2 modal window ActiveForm Ajax form verification of the relevant knowledge, hope to help everyone, if you want to know more content please pay attention to cloud Habitat community website!

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.