如何在流畅的验证中编写规则来检查两个可为空的日期,因为开始日期需要早于结束日期。
我正在思考
RuleFor(c => c.StartDate)
.NotEmpty()
如果开始日期不为空且结束日期不为空则进行比较
像这样的东西-
RuleFor(ac => ac.StartDate)
.NotEmpty().WithMessage("*Required")
RuleFor(ac => ac.EndDate)
.NotEmpty().WithMessage("*Required")
.GreaterThan(r => r.StartDate);
Note-
数据类型必须相同才能进行比较。
或者从此更方便source http://www.onthefencedevelopment.com/blog/validating-startend-dates-fluent-validation-mvc-4-
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)