【问题标题】:Fluent Validator missing SetCollectionValidator() methodFluent Validator 缺少 SetCollectionValidator() 方法
【发布时间】:2014-05-29 21:59:06
【问题描述】:

我是 Fluent Validation 的新手,昨天刚刚从 nu Get 获得 5.3 版。我正在尝试将现有验证器 (PhoneValidator) 应用于类 (Employee) 的集合属性 (ICollection)。 Fluent Validator 文档说要使用:

RuleFor(x => x.Orders).SetCollectionValidator(new OrderValidator()); // example usage

但是 SetCollectionValidator() 方法在我拥有的版本上不可用。相反,只有 SetValidator() 被标记为 [deprecated]。我看过其他关于这种情况的帖子,并且了解到 SetCollectionValidator() 是一种扩展方法,需要确保我导入了 FluentValidation。我愿意。

我在这里错过了什么?

using FluentValidation;
using FluentValidation.Validators;

public class EmployeeValidator : AbstractValidator<Employee>
{
    public EmployeeValidator()
    {            
        // SetCollectionValidator doesn't show in intellisense and won't compile
        RuleFor(e => e.PhoneNumbers).SetCollectionValidator(new PhoneValidator()); 
    }
}

public class PhoneValidator : AbstractValidator<Phone>
{
    public PhoneValidator()
    {
        RuleFor(e => e.Number).Length(10).Matches("^[0-9]$");            
    }
}

【问题讨论】:

  • 能否添加您的 Employee 类(或者至少是 Employee 类中 PhoneNumbers 属性的类型)

标签: c# validation fluentvalidation


【解决方案1】:

我迟到了,但它是 Google 上的第一个热门,所以我认为它会帮助其他人。 API 好像变了,你现在需要做:

RuleForEach(e => e.PhoneNumbers).SetValidator(new PhoneValidator());

在我看来更好一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多