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.