How can I write a rule in fluent validation to check two nullable dates in that the start date needs to be earlier than the end date.
I am thinking along the line of
RuleFor(c => c.StartDate)
.NotEmpty()
if the start date is not empty and end date not empty then compare
Something like this-
RuleFor(ac => ac.StartDate)
.NotEmpty().WithMessage("*Required")
RuleFor(ac => ac.EndDate)
.NotEmpty().WithMessage("*Required")
.GreaterThan(r => r.StartDate);
Note-
The datatypes must be same for comparison.
Or more convinient from this source-
RuleFor(m => m.StartDate)
.NotEmpty()
.WithMessage("Start Date is Required");
RuleFor(m => m.EndDate)
.NotEmpty().WithMessage("End date is required")
.GreaterThan(m => m.StartDate.Value)
.WithMessage("End date must after Start date")
.When(m => m.StartDate.HasValue);
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