ASP.NET MVC 4中的自定义验证摘要

时间:2013-08-05 14:23:24

标签: jquery asp.net-mvc validation

我正在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(一次性所有错误)永远不会触发。

你会怎么处理这个? 感谢

1 个答案:

答案 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