Home>Article>Backend Development> ActiveForm of yii2 modal pop-up window implements asynchronous form verification of ajax

ActiveForm of yii2 modal pop-up window implements asynchronous form verification of ajax

*文
*文 Original
2018-01-02 11:07:47 1826browse

This article mainly introduces the relevant information of ActiveForm ajax form verification of yii2 modal pop-up window. It is very good and has reference value. Friends in need can refer to it. I hope to be helpful.

We will give a simple explanation on how yii2 ActiveForm submits the form in Ajax, which is also the focus of our topic today.

In yii2, ActiveForm performs client-side verification by default, but form submission is not refresh-free. That is to say, it is often seen that the page will refresh after the form is submitted. If you want to enable the refresh-free mode, you only need to enable enableAjaxValidation in ActiveForm, as shown below

 'form-id', 'enableAjaxValidation' => true, ] ); ?>

Note that neither id nor enableAjaxValidation is missing.

Let’s look at the server-side implementation

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, ]);

In this way, we can simply implement the yii2 asynchronous non-refresh submission form!

In fact, it doesn’t matter whether I say it or not. I mainly write it for reference by some lazy people. Smart people will understand after reading the title how to solve the problem of modal submitting forms through ActiveForm.

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

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:

How to use join and joinwith multi-table association queries in Yii2

Yii2 implements QQ Internet login

Yii2 implements rbac permission control

The above is the detailed content of ActiveForm of yii2 modal pop-up window implements asynchronous form verification of ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn