0
说我有类是这样的:有没有办法让FluentValidation停止处理其他规则,而无需为所有规则设置CascadeMode?
public class ParentClass
{
public ChildClass Child { get; set; }
public DateTime Date { get; set; }
}
public class ChildClass
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
和验证程序是这样的:
public class ParentClassValidator : AbstractValidator<ParentClass>
{
public ParentClassValidator()
{
RuleFor(x => x.Child).NotNull();
RuleFor(x => x.Date).Must((parent, date) =>
date < parent.Child.EndDate && date > parent.Child.StartDate);
// ...other rules.
}
}
是否可以告诉FluentValidation停止检查规则,如果第一个失败(如儿童为null) ,但运行所有其他规则,无论失败如果通过?
即我真的希望是这样的:
public ParentClassValidator()
{
RuleFor(x => x.Child).NotNull().StopOnFailure();
RuleFor(x => x.Date).Must((parent, date) => date < parent.Child.EndDate && date > parent.Child.StartDate);
// ...other rules.
}
我知道我可以设置CascadeMode = CascadeMode.StopOnFirstFailure,但将尽一切规则失败终止检查。说实话,在这种情况下,我只是想保存FluentValidation抛出一个异常,因为它试图访问它已经知道为null的parent.Child!
对于本示例,您应该可以使用[When](http://fluentvalidation.codeplex.com/wikipage?title=Customising&referringTitle=Documentation&ANCHOR#WhenUnless),但它需要重复验证和验证之间的非空检查谓词的时候。 –
Bleh。这不像是一个很干净的做法:(我怀疑这是唯一的方法,虽然。 – adhocgeek
我怀疑这也是:见http://fluentvalidation.codeplex.com/discussions/352581 –