Have a form where a user can enter start date/time and end date/time for an event. Here\'s the validator so far:
public class EventModelValidator : AbstractV
Finally got it working after I re-read the documentation: "Note that there is an additional overload for Must that also accepts an instance of the parent object being validated."
public class EventModelValidator : AbstractValidator<EventViewModel>
{
public EventModelValidator()
{
RuleFor(x => x.StartDate)
.NotEmpty().WithMessage("Date is required!")
.Must(BeAValidDate).WithMessage("Invalid date");
RuleFor(x => x.StartTime)
.NotEmpty().WithMessage("Start time is required!")
.Must(BeAValidTime).WithMessage("Invalid Start time");
RuleFor(x => x.EndTime)
.NotEmpty().WithMessage("End time is required!")
.Must(BeAValidTime).WithMessage("Invalid End time")
// new
.Must(BeGreaterThan).WithMessage("End time needs to be greater than start time");
RuleFor(x => x.Title).NotEmpty().WithMessage("A title is required!");
}
private bool BeAValidDate(string value)
{
DateTime date;
return DateTime.TryParse(value, out date);
}
private bool BeAValidTime(string value)
{
DateTimeOffset offset;
return DateTimeOffset.TryParse(value, out offset);
}
// new
private bool BeGreaterThan(EventViewModel instance, string endTime)
{
DateTime start = DateTime.Parse(instance.StartDate + " " + instance.StartTime);
DateTime end = DateTime.Parse(instance.EndDate + " " + instance.EndTime);
return (DateTime.Compare(start, end) <= 0);
}
}
There might be a cleaner/more legant way to do this, but for now, it worksforme.
You could could try using the GreaterThan
rule:
RuleFor(x => x.EndDate)
.GreaterThan(x => x.StartDate)
.WithMessage("end date must be after start date");