您可以为验证设置一个属性并使用该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