尽管在远程验证中存在错误,但Jquery验证仍然有效

时间:2018-06-02 14:11:03

标签: jquery jquery-validate

我有jquery验证。它工作正常,除了检查遥控器。它似乎在表单已经提交后检查远程。

这是我的代码:

$('#m_login_signup_submit').click(function (e) {
    e.preventDefault();

    let btn = $(this);
    let form = $('#register_form');

    form.validate({
        rules: {
            fullname: {
                required: true
            },
            email: {
                required: true,
                email: true,
                remote: '/account/check-email',
            },
            password: {
                required: true
            },
            password_confirmation: {
                required: true,
                equalTo: '#register_password'
            },
            agree: {
                required: true
            }
        },

        messages:{
            email:{remote:'Email already registered'}
        },
    });

    if (form.valid()) {

        btn.addClass('m-loader m-loader--right m-loader--light').attr('disabled', true);
        form.ajaxSubmit({
            url: '/account/register',
            success: function (response, status, xhr, $form) {
                let result = $.parseJSON(response);
                if (result.status === 'success') {

                    form.clearForm();
                    form.validate().resetForm();

                }else{
                    showErrorMsg(form, 'danger', result.message);
                }
            }
        });
    }

});

如果所有验证都通过,包括远程验证,我怎样才能提交表单?

1 个答案:

答案 0 :(得分:0)

  1. 我认为您可以更好地将Ajax放在submitHandler方法的.validate()回调中,而不是在外部进行。 According to the docs,这是“通过Ajax提交表单的正确位置”

  2. 不要将.validate()放在click处理程序中。 .validate()方法用于初始化表单上的插件,不需要在每次单击按钮时调用。您调用一次,插件会自动捕获按钮(必须为type="submit" )。

  3. $(document).ready(function() {
    
        $('#register_form').validate({
            rules: {
                fullname: {
                    required: true
                },
                email: {
                    required: true,
                    email: true,
                    remote: '/account/check-email',
                },
                password: {
                    required: true
                },
                password_confirmation: {
                    required: true,
                    equalTo: '#register_password'
                },
                agree: {
                    required: true
                }
            },
            messages:{
                email:{remote:'Email already registered'}
            },
            submitHandler: function(form) {
                $('#m_login_signup_submit').addClass('m-loader m-loader--right m-loader--light').attr('disabled', true);
                $(form).ajaxSubmit({
                    url: '/account/register',
                    success: function (response, status, xhr, $form) {
                        let result = $.parseJSON(response);
                        if (result.status === 'success') {
                            $(form).clearForm();
                            $(form).validate().resetForm();
                        } else {
                            showErrorMsg(form, 'danger', result.message);
                        }
                    }
                });
            }
        });
    
    });