如果对子代的验证失败,则不会在父代上调用IValidatableObject.Validate [英] IValidatableObject.Validate doesn't get called on parent if validation fails on child

查看:97
本文介绍了如果对子代的验证失败,则不会在父代上调用IValidatableObject.Validate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何详细信息,但是我遇到的问题是,如果在任何子属性上的Validate调用失败,则不会调用父对象的Validate函数.下面是简单的场景:

I can't find any details on this, but the issue I am having is that Validate function of parent object doesn't get called if Validate call fails on any child properties. Simple scenario below:

public class Child : IValidateObject 
{
    public IEnumerable<ValidationResult> Validate(ValidationContext  validationContext)
    { ... } 
}

public class Parent : IValidatableObject
{ 
    public Child Child { get; set;}
    public IEnumerable<ValidationResult> Validate(ValidationContext  validationContext)
    { ... } 
}

如果在子级中的验证失败,则不会调用父级的Validate函数,因此最终您必须先解决所有子级问题,然后再提交,然后才能看到父级的所有验证失败.

If the validation in the child fails then the Validate function of the parent doesn't get called so you end up having to fix all the child issues first then submit and only then will you see all the validation failures of the parent.

如果有人可以帮助我了解发生这种情况的原因,或者指出一些与此相关的文档,那么我会很棒.

If someone can either help me understand why this is happening or point me to some documents regarding this that would be awesome.

推荐答案

根据

According to this code in 2.0.0 branch on GitHub ValidationVisitor will stop validating parent if one of children failed.

在最新的dev分支中,他们引入了新属性ValidateComplexTypesIfChildValidationFails来控制

In latest dev branch they introduced new property ValidateComplexTypesIfChildValidationFails to control this behavior.

Github问题与该问题有关.我不清楚如何设置此新属性.

Github issue related to this question. It's not clear to me how to set this new property.

在Github上创建了新的问题以跟踪此问题.

Created new issue on Github to track this issue.

这篇关于如果对子代的验证失败,则不会在父代上调用IValidatableObject.Validate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆