This article mainly introduced the Yii2 modal window ActiveForm Ajax form verification related data, very good with reference value, the need for friends can refer to. We hope to help you.
We will yii2 ActiveForm how to submit a form in an AJAX manner, which is the focus of our topic today.
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,]);
Related recommendations:
The use of join and Joinwith Multi-table association query in YII2
Yii2 Realize QQ Interconnection Login
YII2 realization of RBAC permission control