検証用のプロパティを設定し、Must
メソッドを使用して適用できます。
RuleFor(p => p.PhoneHome)
.Matches(Regexes.TenDigitsOnly).WithMessage("Phone numbers must have only ten digits.")
.Must((person, phone) => ValidatePhones(person, phone))..WithMessage("Please add a phone number.");
RuleFor(p => p.PhoneWork)
.Matches(Regexes.TenDigitsOnly).WithMessage("Phone numbers must have only ten digits.")
.Must((person, phone) => ValidatePhones(person, phone))..WithMessage("Please add a phone number.");
RuleFor(p => p.PhoneCell)
.Matches(Regexes.TenDigitsOnly).WithMessage("Phone numbers must have only ten digits.")
.Must((person, phone) => ValidatePhones(person, phone))..WithMessage("Please add a phone number.");
private bool ValidatePhones(Person person, string phone) {
return !string.IsNullOrEmpty(person.PhoneHome) || !string.IsNullOrEmpty(person.PhoneWork) || !string.IsNullOrEmpty(PhoneCell);
}
カスタム検証をご覧ください:
http://fluentvalidation.codeplex.com/wikipage?title=Custom