C# Fluent Validator缺少SetCollectionValidator()方法
我是新来的流畅验证,昨天刚从nu Get获得版本5.3。我正在尝试将现有验证器(PhoneValidator)应用于类(Employee)的集合属性(ICollection)。Fluent Validator文档说明要使用:C# Fluent Validator缺少SetCollectionValidator()方法,c#,validation,fluentvalidation,C#,Validation,Fluentvalidation,我是新来的流畅验证,昨天刚从nu Get获得版本5.3。我正在尝试将现有验证器(PhoneValidator)应用于类(Employee)的集合属性(ICollection)。Fluent Validator文档说明要使用: RuleFor(x => x.Orders).SetCollectionValidator(new OrderValidator()); // example usage 但是,SetCollectionValidator()方法在我的版本上不可用。相反,只有标记为
RuleFor(x => x.Orders).SetCollectionValidator(new OrderValidator()); // example usage
但是,SetCollectionValidator()方法在我的版本上不可用。相反,只有标记为[不推荐]的SetValidator()。我看过其他关于这种情况的帖子,了解到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]$");
}
}
使用FluentValidation;
使用FluentValidation.validator;
公共类EmployeeValidator:AbstractValidator
{
公共雇员验证器()
{
//SetCollectionValidator不会在intellisense中显示,也不会编译
RuleFor(e=>e.PhoneNumbers).SetCollectionValidator(新的PhoneValidator());
}
}
公共类PhoneValidator:AbstractValidator
{
公共电话验证器()
{
RuleFor(e=>e.Number).Length(10).Matches(“^[0-9]$”);
}
}
我来晚了,但这是谷歌的第一次成功,所以我认为这会帮助其他人。API似乎已更改,您现在需要执行以下操作:
RuleForEach(e=>e.PhoneNumbers).SetValidator(新的PhoneValidator());
在我看来更好一点。您可以添加Employee类(或者至少添加Employee类中PhoneNumber属性的类型)吗