使用流畅验证在 EITHER ... OR ... 情况下允许为空的验证规则

HIT*_*IYA 0 asp.net validation asp.net-mvc nopcommerce fluentvalidation

如何在电子邮件或电话号码中编写允许为空的验证规则

RuleFor(x => x.Email).NotEmpty().WithMessage(localizationService.GetResource("ContactUs.Email.Required"));
RuleFor(x => x.PhoneNumber).NotEmpty().WithMessage(localizationService.GetResource("Products.MakeAnOffer.PhoneNumber"));
Run Code Online (Sandbox Code Playgroud)

Rom*_*ada 5

尝试这个:

    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"));
Run Code Online (Sandbox Code Playgroud)