I'd like with FluentValidation check the email format. The email is not mandatory. Then I have to check only when the property is not empty.How can I do this ? Below I check all the time.
RuleFor(x => x.Email)
.EmailAddress()
.WithLocalizedMessage(() => "My message.");
Thanks
Use where or unless.
RuleFor(x => x.Email)
.EmailAddress()
.WithLocalizedMessage(() => "My message.")
.Unless(x => string.IsNullOrEmpty(x.Email));
EDIT: Updated documentation link.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With