Иметь форму, в которой пользователь может ввести дату / время начала и дату / время окончания для события.Вот такой валидатор:
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");
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);
}
}
Теперь я также хотел бы добавить подтверждение того, что EndDateTime> StartDateTime (объединенные свойства Date + Time), но не уверен, как это сделать.
Редактировать: Чтобы уточнить, мне нужно как-то объединить EndDate + EndTime / StartDate + StartTime, т.е. DateTime.Parse (src.StartDate + "" + src.StartTime), а затем проверить EndDateTime против StartDateTime - какЯ так делаю?