有一个表格,用户可以输入事件的开始日期/时间和结束日期/时间。这里的验证至今:FluentValidation - 验证多个属性
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(合并日期+时间属性),但不知道如何去做。
编辑: 为了澄清,我需要以某种方式合并结束日期+结束时间/起始日期+开始时间即DateTime.Parse(src.StartDate + “” + src.StartTime),然后验证EndDateTime对比的startDateTime - 怎么办我这样做?
是否有可能做同样的客户端? – SMC
这在FluentValitation中不再有效。这是正确的答案:http://stackoverflow.com/a/20546097/59119 – Natrium