IValidatableObject.Validate doesn't get called on parent if validation fails on child

巧了我就是萌 提交于 2020-02-15 09:43:07

问题


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)
    { ... } 
}

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.


回答1:


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

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

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

Created new issue on Github to track this issue.



来源:https://stackoverflow.com/questions/49122976/ivalidatableobject-validate-doesnt-get-called-on-parent-if-validation-fails-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!