一条关于规则链的消息? [英] One Message for rule chain?

查看:93
本文介绍了一条关于规则链的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在FluentValidation中遇到问题,无论给定链中的验证错误如何,我都希望显示一条消息.例如,我为下面的一个属性定义了一个验证链.我希望对链进行评估,任何失败都会导致在下面的 WithMessage()调用中定义消息.但是,这似乎是短路,并且仅针对遇到的第一个错误显示FluentValidation默认错误消息.参见下面的代码:

I'm having an issue with FluentValidation where I want to display one message regardless of the validation error in a given chain. For example, I've defined a validation chain for one property below. I would expect that the chain would be evaluated and any failures would result in the message defined in the WithMessage() call below. However, it seems that it's short-circuiting and only displaying the FluentValidation default error message for the first error encountered. See code below:

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.Age Min'不能为空".投标详细信息是封装视图模型的名称.我尝试将整个验证器的CascadeMode设置为CascadeMode.Continue,但没有效果.

What's happening is that the AgeMin property is null, so the first NotNull() check is failing and the validation message reads "'Proposal Detail. Age Min' must not be empty." Proposal Detail is the name of the encapsulating view model. I've tried setting the CascadeMode for the entire validator to CascadeMode.Continue, but it has no effect.

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

How can I accomplish one message for one property validation chain?

推荐答案

更新2

事实证明,您可以使用简单的扩展方法来完成所需的操作

As it turns out,you can accomplish what you want with a simple extension method

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:(2019年4月4日)在FluentValidation v8.2.2中, IRuleBuilderOptions 接口不再具有直接访问 IRuleBuilderOptions.ErrorMessageSource 属性的权限,而应使用: IRuleBuilderOptions.Options.ErrorMessageSource .

Update 3: (Apr 04/07/2019) In FluentValidation v8.2.2, The IRuleBuilderOptions interface do not have direct access to IRuleBuilderOptions.ErrorMessageSource property anymore, instead we should use: IRuleBuilderOptions.Options.ErrorMessageSource .

这篇关于一条关于规则链的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆