2015-08-09 97 views
0

我的AJAX信息非常少。这就是为什么我在这里问我的问题...Yii2 ajax验证并提交表格

in yii2如何创建和提交表单与Ajax验证?

所以我搜查,但没有找到合适的解决方案...

例如:(根据官方培训现场)

<?php $form = ActiveForm::begin([ 
      'id' => $model->formName(), 
      'enableAjaxValidation' => true, 
]); ?> 


<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?> 

<?= $form->field($model, 'extra_txt')->textInput(['maxlength' => true]) ?> 

<div class="form-group"> 
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
</div> 

<?php ActiveForm::end(); ?> 

和控制器:

if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { 
     Yii::$app->response->format = 'json'; 
     return ActiveForm::validate($model); 
    } else { 
     return $this->render('create', [ 
      'model' => $model, 
     ]); 
    } 

我的问题:

1- ajax验证

2-使用ajax提交表格

回答