我正在ASP.NET MVC 4应用程序中进行客户端验证。我正在尝试从不显眼的jquery验证中覆盖默认验证摘要。有一个方法“OnErrors”,我需要覆盖它。我找到了一个解决方案,但对我来说它只适用于单个错误。这是代码:
$(function () {
var settings = $.data($('form')[0], 'validator').settings;
var oldErrorFunction = settings.errorPlacement;
var oldErrorsFunction = settings.invalidHandler;
var oldSucessFunction = settings.success;
settings.invalidHandler = function (form, validator) {
alert("Form is invalid");
oldErrorsFunction(form, validator);
}
settings.errorPlacement = function (error, inputElement) {
$(".validation-summary-errors").append("single error ");
var errorsNumber = 0;
$(".validation-summary-errors li").each(function () {
errorsNumber++;
});
oldErrorFunction(error, inputElement);
}
settings.success = function (error) {
oldSucessFunction(error);
}
});
errorPlacement(对于每个错误)都运行良好,但invalidHandler(一次性所有错误)永远不会触发。
你会怎么处理这个? 感谢
答案 0 :(得分:3)
我们为此工作:
form.unbind("invalid-form.validate", form.validate().settings.invalidHandler); // Unbind the existing invalidHandler
form.validate().settings.invalidHandler = $.proxy(customValidationSummary, form); // Replace invalidHandler with a custom one
form.bind("invalid-form.validate", form.validate().settings.invalidHandler); // Rebind the new invalidHandler