【发布时间】:2016-06-23 17:55:44
【问题描述】:
我有这个流畅的验证规则:
RuleForEach(tvm => tvm.Task.Executors).Cascade(CascadeMode.StopOnFirstFailure).Must((tvm, exec) => { return exec.Deadline.HasValue ? exec.Deadline.Value.Date >= DateTime.Now.Date : true; }).
When(tvm=>!tvm.Task.Instructed).
WithMessage("Deadline can't be earlier than today").
Must((tvm,exec)=>{ return exec.Deadline.HasValue ? exec.Deadline.Value.Date >= tvm.Task.InstructDate.Value.Date : true; }).
When(tvm=>tvm.Task.Instructed).
WithMessage("Deadline can't be earlier than the instructed date").
Must((tvm, exec) => { return exec.InstructionId == (int)Instructions.TakeOwnership ? exec.Deadline != null : true; }).
WithMessage("Enter deadline");
如您所见,有 3 条必须规则。前 2 个与 When 条件相关。
我遇到的问题是,第二个 When 条件会影响第一个 Must 规则。例如,如果tvm.Task.Instructed 为假,输入的Deadline 为2016-06-22(并考虑到当前日期为2016-06-23),我希望收到Deadline can't be earlier than today 消息。但我没有收到该消息,因为第二个When 检查tvm.Task.Instructed 是否为真并返回假。所以看起来当条件不仅影响它遵循的规则。我真的很想用一行流利地写这些规则。这是可能的吗,或者除了单独定义它们之外我别无选择。
【问题讨论】:
标签: asp.net-mvc conditional-statements fluentvalidation