我有一种使用Yii2 ajax
响应的形式来验证唯一实体。现在,我必须添加一些自定义jQuery验证并添加Error Message and Error Class using jQuery
,但是由于此字段未由Yii2本身验证,因此ajax删除了我由jQuery添加的错误消息和类。有人可以帮我停止Yii2 ajax
删除此自定义验证。
我无法删除Yii2 Ajax,因为如果这样做,它将无法在模糊时验证唯一验证。
这是一些示例代码。
来自我的_form.php
$('#w0').on('beforeSubmit', function (z) {
$(this).find('input.required').each(function () {
$(this).on('blur', function () {
if ($(this).val() == '') {
$(this).closest('div.form-group').addClass('has-error');
$(this).closest('div.form-group').find('p').html('This field can not be blank.');
return false;
}
});
});
});
在控制器内部,我有以下几行
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
答案 0 :(得分:0)
这是自相矛盾的,提交时会调用您的JS事件,但是内部行有blur
事件,该事件不会发生。因为提交时您没有从一个字段移到另一个字段。
这不是YII代码,而是您的JS函数,这是错误的。
尝试以下代码,它应该可以工作:
$('#w0').on('beforeSubmit', function (z) {
$(this).find('input.required').each(function () {
if ($(this).val() == '') {
z.preventDefault();
$(this).closest('div.form-group').addClass('has-error');
$(this).closest('div.form-group').find('p').html('This field can not be blank.');
}
});
});