douyouqian8550 2018-12-13 12:45
浏览 98

Yii2 ajax删除自定义jquery validatiton

I have an form in which I am using Yii2 ajax response to validate unique entities. Now, I have to add some custom jQuery validation and add Error Message and Error Class using jQuery but as this field is not validated by Yii2 itself, ajax removes the error message and class which I added by jQuery. May someone help me to stop Yii2 ajax to remove this custom validation.

I can't remove Yii2 Ajax because if I do so, it will not validate unique validation on blur.

here is some sample code.

from my _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;
            }
        });
    });
});

Inside the controller, I have the following lines

if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
    Yii::$app->response->format = Response::FORMAT_JSON;
    return ActiveForm::validate($model);
}
  • 写回答

1条回答 默认 最新

  • douqiang1910 2018-12-13 12:56
    关注

    This is contradictory, your JS event is called when submitted, but the inner line has blur event, which will not happen. Because you are not moving from one field to another when you are submitting.

    So it's not YII code, but it's your JS functions, which is wrong.

    Give the following code a try, it should work:

    $('#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.');
            }
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序