在jquery.validate.unobtrusive中覆盖ErrorPlacement函数的正确方法是什么?
查看jquery.validate.unobtrusive.js脚本,看起来开发人员的意图是允许您通过设置$jQval.unobtrusive.options来应用自己的ErrorPlacement函数.
在定义errorPlacement函数的validationInfo(form)函数中,我们看到对execInContext(“errorPlacement”,arguments)的调用.
看来如果我们在$.validator.unobtrusive.options下创建一个errorPlacement函数,那么就会调用它.
$.validator.unobtrusive.options = {
errorPlacement: function () {
console.log("hello");
}
};
问题是必须在jquery.validate.js之后和引用jquery.validate.unobtrusive.js之前配置它.否则$jQval.unobtrusive.options为null,并且不会再次设置$form.data(data_validation,result).
function validationInfo(form) {
var $form = $(form),result = $form.data(data_validation),onResetProxy = $.proxy(onReset,form),defaultOptions = $jQval.unobtrusive.options || {},execInContext = function (name,args) {
var func = defaultOptions[name];
func && $.isFunction(func) && func.apply(form,args);
}
if (!result) {
result = {
options: { // options structure passed to jQuery Validate's validate() method
errorClass: defaultOptions.errorClass || "input-validation-error",errorElement: defaultOptions.errorElement || "span",errorPlacement: function () {
onError.apply(form,arguments);
execInContext("errorPlacement",arguments);
},invalidHandler: function () {
onErrors.apply(form,arguments);
execInContext("invalidHandler",messages: {},rules: {},success: function () {
onSuccess.apply(form,arguments);
execInContext("success",arguments);
}
},attachValidation: function () {
$form
.off("reset." + data_validation,onResetProxy)
.on("reset." + data_validation,onResetProxy)
.validate(this.options);
},validate: function () { // a validation function that is called by unobtrusive Ajax
$form.validate();
return $form.valid();
}
};
$form.data(data_validation,result);
}
return result;
}
>设置$.validator.unobtrusive.options错误放置功能
>删除Unobtrusive验证
>重新应用不显眼的验证
$.validator.unobtrusive.options = {
errorPlacement: function () {
console.log("errorPlacement");
}
};
$("#form_selector").removeData("unobtrusiveValidation");
// reapply the form's validator
$.validator.unobtrusive.parse(document);
var validator = $("#form_selector").data('validator');
var originalFunction = validator.settings.errorPlacement;
validator.settings.errorPlacement = function(error,element) {
console.log("errorPlacement");
originalFunction(error,element);
};
$.validator.unobtrusive.options = {
errorPlacement: function () {
console.log("hello");
}
};
它工作正常,但还有一个额外的问题:这条线必须在文件准备好之前运行.通过这个,从jquery.validate.unobtrusive.js的底部调用validationInfo方法在文档就绪:
$(function () {
$jQval.unobtrusive.parse(document);
});
解析最终调用validationInfo.不显眼的代码的问题是validationInfo设置defaultOptions = $jQval.unobtrusive.options || {}.很明显,如果你还没有定义unobtrusive.options,那么无论你对$jQval.unobtrusive.options做什么,它都会愚蠢地将defaultOptions设置为空白对象并且永远不会再次检查它.如果他们只是删除了|| {}一切都会很好,因为它只是一个对象引用.唉…
如果在加载文档之前无法设置errorPlacement回调,或者需要动态更改它,只需在jquery.validate.js和jquery.validate.unobtrusive.js之后加载的一个脚本中添加这一行:
$.validator.unobtrusive.options = {};
这将导致defaultOptions指向您可以引用的对象.然后,在您需要的任何地方,按照您的预期更改$.validator.unobtrusive.options.注意不要覆盖对象,因为如果你这样做,你不会影响defaultOptions指向的对象.即,这样做:
$.validator.unobtrusive.options.errorPlacement: function () {
console.log("hello");
};