0

How to write a Validation rule for Allow Empty in EITHER Email OR in PhoneNumber

RuleFor(x => x.Email).NotEmpty().WithMessage(localizationService.GetResource("ContactUs.Email.Required"));
RuleFor(x => x.PhoneNumber).NotEmpty().WithMessage(localizationService.GetResource("Products.MakeAnOffer.PhoneNumber"));
Roman Koliada
  • 4,286
  • 2
  • 30
  • 59

1 Answers1

5

Try this:

    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"));
Roman Koliada
  • 4,286
  • 2
  • 30
  • 59