开发者

jQuery Validation - Overwriting core validation method

I'm using the jQuery Validation plug-in.

I have overwritten the required rule since mine depends on some custom condition as in following:

$.validator.addMethod("required", function(value, element) {
    console.log("required", element);

    return someCondition && value != null;
}, "This is is required.开发者_高级运维");

When the form is validated however, I see that "required" is printed twice for each element meaning the function is called twice. As soon as I change the method name to "myRequired" and add the following class rule, the function is called just once as it should be.

$.validator.addClassRules({
    required : {
        myRequired: true
    }
});

It appears that the core required method is still intact and called even if I add a method with the same name. Is there anyway to entirely overwrite the core rule method?


You're not overriding it, but adding another method to it. To override it, try this:

$.validator.methods.required = function(value, element, param) {
    console.log("required", element);

    return someCondition && value != null;
}


You can use $.extend to do this:

$.extend($.validator, {
    required: function(value, element) {
        console.log("required", element);
        return someCondition && value != null;
    }
})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜