Home  >  Article  >  Backend Development  >  Yii2 implements ActiveForm ajax submission

Yii2 implements ActiveForm ajax submission

不言
不言Original
2018-05-03 15:58:491780browse

This article mainly This article introduces the relevant information of Yii2 to implement ActiveForm ajax submission in detail, which has certain reference value. Interested friends can refer to

When doing projects, you will always encounter the ajax submission function, especially in When doing background submission, the model is usually automatically generated. This function is used more frequently. In fact, as long as you understand the process, the operation is quite simple and convenient to use.

Form part

<?php $form = ActiveForm::begin([ 
  &#39;action&#39; => [&#39;save&#39;], //提交地址(*可省略*) 
  &#39;method&#39;=>&#39;post&#39;,  //提交方法(*可省略默认POST*) 
  &#39;id&#39; => &#39;form-save&#39;, //设置ID属性 
  &#39;options&#39; => [ 
    &#39;class&#39; => &#39;form-horizontal&#39;, //设置class属性 
  ], 
  &#39;enableAjaxValidation&#39; => true, 
  &#39;validationUrl&#39; => &#39;validate-view&#39;, 
]); ?> 
 
<?php echo $form->field($model,&#39;company_name&#39;, [&#39;inputOptions&#39; => [&#39;placeholder&#39;=>&#39;请输入商家名称&#39;,&#39;class&#39; => &#39;form-control&#39;], &#39;template&#39;=>&#39;<label for="inputCompanyName" class="col-sm-1 control-label"><span class="text-red">*</span> 商家名称</label><p class="col-md-8">{input}</p><label class="col-sm-3" for="inputError">{error}</label>&#39;])->textInput()?> 
 
<?=Html::submitButton(&#39;保存&#39;,[&#39;class&#39;=>&#39;btn btn-primary&#39;]); ?> 
 
<?php ActiveForm::end(); ?>

Among them: 'enableAjaxValidation' => true, must be set to tell the form to use ajax Submit

Controller part

The controller is divided into two parts, one part is to verify the correctness of the form, and the other part is to save

1 , Validation part

public function actionValidateView() 
{ 
  $model = new model(); 
  $request = \Yii::$app->getRequest(); 
  if ($request->isPost && $model->load($request->post())) { 
    \Yii::$app->response->format = Response::FORMAT_JSON; 
    return ActiveForm::validate($model); 
  } 
}

##2. Save part

public function actionSave() 
{ 
  \Yii::$app->response->format = Response::FORMAT_JSON; 
  $params = Yii::$app->request->post(); 
  $model = $this->findModel($params[id]); 
 
  if (Yii::$app->request->isPost && $model->load($params)) { 
    return [&#39;success&#39; => $model->save()]; 
  } 
  else{ 
    return [&#39;code&#39;=>&#39;error&#39;]; 
  } 
}

Ajax submission from form

$(function(){ 
$(document).on(&#39;beforeSubmit&#39;, &#39;form#form-save&#39;, function () { 
    var form = $(this); 
    //返回错误的表单信息 
    if (form.find(&#39;.has-error&#39;).length) 
    { 
      return false; 
    } 
    //表单提交 
    $.ajax({ 
      url  : form.attr(&#39;action&#39;), 
      type  : &#39;post&#39;, 
      data  : form.serialize(), 
      success: function (response){ 
        if(response.success){ 
          alert(&#39;保存成功&#39;); 
          window.location.reload(); 
        } 
      }, 
      error : function (){ 
        alert(&#39;系统错误&#39;); 
        return false; 
      } 
    }); 
    return false; 
  }); 
});

Specially note that I am using the Yii2 adminlte framework background, the specific operation process It depends on the trial project, but the basic operation process is the same.

Related recommendations:


Yii2 form event Ajax submission implementation method

The above is the detailed content of Yii2 implements ActiveForm ajax submission. 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