jQuery验证插件-应在懒惰时对自定义方法进行急切验证 [英] JQuery Validate Plugin - Eager validation on custom method when should be lazy

查看:83
本文介绍了jQuery验证插件-应在懒惰时对自定义方法进行急切验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

validate插件似乎很想在我拥有的自定义方法上进行验证,而该方法应该是惰性的,而其他所有验证都是惰性进行的(即直到提交表单之前).

The validate plugin seems to be eagerly validating on a custom method I have when it should be lazy, all other validation occurs lazily (i.e. not until the form is submitted).

自定义方法:

$.validator.addMethod("refDataAcInput", function(value, element)
{
    return ($(element).val() == "" || $(element).data("hasValidSelectedValue") != null);
}, "The item must be a valid selected item.");

验证插件的初始化:

this.$form.validate({
    ignore: null,
    invalidHandler : function()
    {
        _self.initUnsavedChangesWarning.ignorePageLeaveReq = false;
        _self.setValidationMsgVisible(true);

        $("body,html").scrollTop($("#cmFormErrorReport").position().top);
    },
    submitHandler :function(form)
    {
        //Disable form submit button - prevent duplicate request for impatient users
        $("button[type=submit]", form).prop("disabled", true);
        form.submit();
    },
    showErrors : function(errorMap, errorList)
    {
        _self.updateErrors(errorList);
        this.defaultShowErrors();
    },
    errorPlacement : function(error, $element)
    {
        $element.parents("tr").children(".fieldError").append(error);
    },
    errorClass : "jqueryError"
});

有什么主意如何让它懒惰地发生吗?

Any ideas how to make this occur lazily?

推荐答案

问题是您已将required规则构建到自定义方法中.

The problem is that you've built the required rule into your custom method.

$.validator.addMethod("refDataAcInput", function(value, element) {
    return ($(element).val() == "" || $(element).data("hasValidSelectedValue") != null);
}, "The item must be a valid selected item.");

删除$(element).val() == ""并替换为this.optional(element) ...

Remove $(element).val() == "" and replace with this.optional(element)...

$.validator.addMethod("refDataAcInput", function(value, element) {
    return (this.optional(element) || $(element).data("hasValidSelectedValue") != null);
}, "The item must be a valid selected item.");

然后,如果您还希望该字段为required,只需声明required规则以及自定义的refDataAcInput规则即可.

Then if you also want the field to be required, just declare the required rule along with your custom refDataAcInput rule.

该插件的默认懒惰"验证现在应该可以按预期工作.

The plugin's default "Lazy" validation should now be working as expected.

这篇关于jQuery验证插件-应在懒惰时对自定义方法进行急切验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆