【发布时间】:2018-06-12 04:41:16
【问题描述】:
我制作了一个带有验证和 ajax 提交的活动表单。
在视图中创建表单:
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'layout' => 'horizontal',
'method' => 'post',
'enableClientValidation' => false,
'enableAjaxValidation' => true,
'validationUrl' => 'panel/validate',
'fieldConfig' => [
'options' => [
'tag' => false,
],
'template' =>'{input}<p class="help-block help-block-error ">{error}</p>'
],
]); ?>
验证动作:
public function actionValidate()
{
$model = new LoginForm();
$request = \Yii::$app->getRequest();
if ($request->isAjax && $request->isPost && $model->load(Yii::$app->request->post())) {
\Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
return $this->renderAjax('login', [
'model' => $model,
]);
}
例如,当我将字段留空或不指定验证码时,我可以看到 ajax 响应:
{"loginform-username":["Username cannot be blank."],"loginform-password":["Password cannot be blank."],"loginform-captcha":["Captcha cannot be blank."]}
但是,这些错误没有显示在我的表单字段下。表单字段是这样创建的:
<?= $form->field($model, 'username')->textInput()
如果我关闭 ajax 验证,则会显示错误。这里有什么问题?
【问题讨论】: