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!