Search code examples
asp.net-mvcfluentvalidationbusiness-rules

How do I say I want one out of three FluentValidation rules to be applied?


I have a set of phone numbers, and not everybody has one of each of these numbers. How can I specify that any of the three being satisfied is good enough:

RuleFor(p => p.PhoneHome).Matches(Regexes.TenDigitsOnly).WithMessage("Phone numbers must have only ten digits.");
RuleFor(p => p.PhoneWork).Matches(Regexes.TenDigitsOnly).WithMessage("Phone numbers must have only ten digits.");
RuleFor(p => p.PhoneCell).Matches(Regexes.TenDigitsOnly).WithMessage("Phone numbers must have only ten digits.");

Solution

  • Set up a custom validator that takes in the Model itself rather than just a property on it: http://fluentvalidation.codeplex.com/wikipage?title=Custom