JQuery Validate Plugin - robust validation on custom method when should be lazy

The validate plugin seems to readily check on the user method that I have, when it should be lazy, all other checks are done lazily (i.e. only before the form is submitted).

Custom method:

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

Confirm plugin initialization:

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 do this happen lazily?

+3
source share
1 answer

The problem is that you built the rule requiredin your own method.

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

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.

"Lazy" .

+2

All Articles