Allow Empty in EITHER ... OR ... 使用流畅验证的验证规则

Validation rule for Allow Empty in EITHER ... OR ... case using fluent validation

如何在 EITHER Email 或 PhoneNumber 中编写允许为空的验证规则

RuleFor(x => x.Email).NotEmpty().WithMessage(localizationService.GetResource("ContactUs.Email.Required"));
RuleFor(x => x.PhoneNumber).NotEmpty().WithMessage(localizationService.GetResource("Products.MakeAnOffer.PhoneNumber"));

试试这个:

    RuleFor(x => x.Email)
        .NotEmpty()
        .When(x => string.IsNullOrEmpty(x.PhoneNumber))//will run only if PhoneNumber is empty
        .WithMessage(localizationService.GetResource("ContactUs.Email.Required"));

    RuleFor(x => x.PhoneNumber)
        .NotEmpty().When(x => string.IsNullOrEmpty(x.Email))//will run only if Email is empty
        .WithMessage(localizationService.GetResource("Products.MakeAnOffer.PhoneNumber"));