Heinrich
Heinrich

Reputation: 2234

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

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.

Upvotes: 3

Views: 1629

Answers (1)

Mike
Mike

Reputation: 4051

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.

Upvotes: 4

Related Questions