【发布时间】: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