Yii2 modal the activeform of pop-up window to implement AJAX form asynchronous validation

Source: Internet
Author: User
Tags yii
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.