规则链的一条消息?

时间:2016-02-19 19:12:19

标签: c# .net fluentvalidation

我遇到了FluentValidation的问题,我想显示一条消息,而不管给定链中的验证错误。例如,我已经为下面的一个属性定义了一个验证链。我希望会对链进行评估,任何失败都会导致下面WithMessage()调用中定义的消息。但是,它似乎是短路的,只显示遇到的第一个错误的FluentValidation默认错误消息。请参阅以下代码:

RuleFor(s => s.ProposalDetail.AgeMin).NotNull()
        .GreaterThanOrEqualTo(1)
        .LessThanOrEqualTo(99)
        .WithMessage("Minimum Age entry is required and must range from 1 to 99 years.");

发生的事情是AgeMin属性为null,因此第一个NotNull()检查失败,验证消息显示为“'Proposal Detail.Ell Min'不能为空。” Proposal Detail是封装视图模型的名称。 我已经尝试将整个验证器的CascadeMode设置为CascadeMode.Continue,但它没有效果。

如何为一个属性验证链完成一条消息?

3 个答案:

答案 0 :(得分:10)

更新2

事实证明,您可以使用简单的扩展方法完成您想要的任务

using FluentValidation;
using FluentValidation.Internal;
using FluentValidation.Resources;
using FluentValidation.Results;
using System;
using System.Linq;

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {

            Customer customer = new Customer() { };
            CustomerValidator validator = new CustomerValidator();
            ValidationResult results = validator.Validate(customer);
            Console.WriteLine(results.Errors.First().ErrorMessage);
            Console.ReadLine();
        }
    }
    public class CustomerValidator : AbstractValidator<Customer>
    {
        public CustomerValidator()
        {

            RuleFor(s => s.Id).NotNull()
                          .GreaterThanOrEqualTo(1)
                          .LessThanOrEqualTo(99)
                          .WithGlobalMessage("Minimum Age entry is required and must range from 1 to 99 years.");
        }

    }

    public class Customer { public int? Id { get; set; } }

    public static class MyExtentions
    {
        public static IRuleBuilderOptions<T, TProperty> WithGlobalMessage<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, string errorMessage)
        {
            foreach (var item in (rule as RuleBuilder<T, TProperty>).Rule.Validators)
                item.Options.ErrorMessageSource=new StaticStringSource(errorMessage);

            return rule;
        }
    }
}

更新3:(Apr 04/07/2019) 在FluentValidation v8.2.2中,IRuleBuilderOptions界面无法再直接访问IRuleBuilderOptions.ErrorMessageSource属性,而应使用:IRuleBuilderOptions.Options.ErrorMessageSource

答案 1 :(得分:3)

最直接的解决方案是将消息设置为变量,并在每个规则后应用相同的消息:

var message = "Minimum Age entry is required and must range from 1 to 99 years.";
RuleFor(s => s.ProposalDetail.AgeMin)
    .NotNull()
        .WithMessage(message)
    .GreaterThanOrEqualTo(1)
        .WithMessage(message)
     .LessThanOrEqualTo(99)
        .WithMessage(message);

答案 2 :(得分:0)

替换 ErrorMessageSource 的过时属性分配,并使用波纹管代替 FluentValidation 版本 >= 9.x

    public static class ValidatorExtensions
    {
        public static IRuleBuilderOptions<T, TProperty> WithGlobalMessage<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule, string errorMessage)
        {
            var rules = rule as RuleBuilder<T, TProperty>;
            foreach (var item in rules.Rule.Validators)
            {
                item.Options.SetErrorMessage(errorMessage);
            }

            return rule;
        }
    }