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!