Yii2 ajax 验证并提交表单
Yii2 ajax validate and submit form
我AJAX的信息很少。这就是为什么我在这里问我的问题...
在 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
提交表单
你为什么不使用 Pjax?使用 yii\widgets\Pjax 并在表单周围添加 Pjax::begin() 和 Pjax::end()。
这里有一个不错的指南:
http://www.yiiframework.com/wiki/772/pjax-on-activeform-and-gridview-yii2/
我AJAX的信息很少。这就是为什么我在这里问我的问题...
在 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
提交表单你为什么不使用 Pjax?使用 yii\widgets\Pjax 并在表单周围添加 Pjax::begin() 和 Pjax::end()。
这里有一个不错的指南: http://www.yiiframework.com/wiki/772/pjax-on-activeform-and-gridview-yii2/