简体   繁体   English

MVC JQuery非侵入式验证

[英]MVC JQuery unobtrusive validation

How do i override the default error message in unobtrusive validation?. 如何在非侵入式验证中覆盖默认错误消息?

i tried to use $.validator.unobtrusive.messages.dobvalidation to the reset the error message for that validation type but without any success. 我尝试使用$ .validator.unobtrusive.messages.dobvalidation重置该验证类型的错误消息,但未成功。

    //DOB Validation
    jQuery.validator.unobtrusive.adapters.add("dobvalidation", function (options) {
            options.rules["dobvalidation"] = "true";
            options.messages["dobvalidation"] = options.message;
    });

    jQuery.validator.addMethod("dobvalidation",
        function (value, element, param) {
            if (value.length > 3) {
                var test = $(element).attr('data-val-dobvalidation-err1');
                $.validator.unobtrusive.messages.dobvalidation = test; // this line of code is 
                                                                          not working
                return false;
            }
            return true;
        });



public class DateOfBirthFluentValidationPropertyValidator : FluentValidationPropertyValidator
{
    public int? MinimumAge { get; set; }
    public string DateFormat { get; set; }
    public string InvalidDateFormatErrorMessage { get; set; }
    public string YoungerThanMinimumErrorMessage { get; set; }

    public DateOfBirthFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator,
         int? minimumAge = null,  string dateFormat = null,string invalidDateFormatErrorMessage = null, string youngerThanMinimumAgeErrorMessage = null)
        : base(metadata, controllerContext, rule, validator)
    {
        MinimumAge = minimumAge;
        DateFormat = dateFormat;
        InvalidDateFormatErrorMessage = invalidDateFormatErrorMessage;
        YoungerThanMinimumErrorMessage = youngerThanMinimumAgeErrorMessage;
    }


    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        var rule = new ModelClientValidationRule
        {
            ValidationType = "dobvalidation",
            ErrorMessage = InvalidDateFormatErrorMessage
       };

        rule.ValidationParameters.Add("err1", InvalidDateFormatErrorMessage);

        yield return rule;
    }

}

While its the tedious and often the unusual thing to do, since all of it is controlled through the JS file and it overrides the options that are set explicitly. 尽管这很繁琐且通常是不寻常的事情,但是由于所有内容都是通过JS文件控制的,因此它会覆盖显式设置的选项。 You can refer to these links which might provide you more insight on this. 您可以参考这些链接,这可能会为您提供更多的见解。

Override Unobtrusive JS Validation - 1 覆盖不干扰JS验证-1

Override Unobtrusive JS Validation - 2 覆盖不干扰JS验证-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM