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

*文
Release: 2023-03-18 21:24:01
Original
1885 people have browsed it

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

<?php $form = ActiveForm::begin([
&#39;id&#39; => &#39;form-id&#39;,
&#39;enableAjaxValidation&#39; => true,
]
); ?>
Copy after login

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([&#39;index&#39;]); 
}
}
}
return $this->render(&#39;create&#39;, [
&#39;model&#39; => $model,
]);
Copy after login

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 [&#39;success&#39; => true];
}
return $this->redirect([&#39;index&#39;]);
} 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(&#39;create&#39;, [
&#39;model&#39; => $model,
]);
} else {
return $this->render(&#39;create&#39;, [
&#39;model&#39; => $model,
]);
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!