I'm new to Fluent Validation and just got the version 5.3 from nu Get yesterday. I'm trying to apply an existing validator (PhoneValidator) to a collection property (ICollection) of a class (Employee). The Fluent Validator documentation says to use:
RuleFor(x => x.Orders).SetCollectionValidator(new OrderValidator()); // example usage
However the SetCollectionValidator() method is not available on the version I have. Instead there is only SetValidator() which is marked as [deprecated]. I've seen other posts regarding this same situation and have learned that SetCollectionValidator() is an extension method and need to be sure I have FluentValidation imported. I do.
What am I missing here?
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]$");
}
}
I'm late to the party but it was the first hit on Google so I thought it'd help others. It seems the API has changed, you now need to do:
RuleForEach(e => e.PhoneNumbers).SetValidator(new PhoneValidator());
A bit nicer in my opinion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With