3

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.

Heinrich
  • 2,144
  • 3
  • 23
  • 39
  • Shouldn't you be validating on the parent anyway and inside `Validate` call your child objects aggregating all the results along with the parent –  Mar 06 '18 at 03:48
  • @MickyD What happens when the child object is part of multiple parents, that would mean I would have to duplicate the validation functionality in every class that consumes the child. Secondly that doesn't play nice if Child was a list :/ – Heinrich Mar 06 '18 at 03:52
  • It is not the purpose of `Validate` to know about your object hierarchy and automatically call `Validate` in each object sadly. –  Mar 06 '18 at 04:07
  • That is absolutely fine, my question however is not about the hierarchy it is about why the Validate function of parent doesn't get triggered if there is a validation error in the child. If what you say is true shouldn't it ignore the hierarchy and run every Validate function it encounters? – Heinrich Mar 06 '18 at 04:17
  • @Heinrich I just checked and it works in ASP.NET Core 2.0 – Mike Mar 06 '18 at 04:25
  • 1
    The behavior is by design. –  Mar 06 '18 at 04:27
  • @MikeMazmanyan so in core 2.0 the parent will validate even if there is validation error on the child? Excellent if I was using 2.0 :( Thanks tho – Heinrich Mar 06 '18 at 04:31
  • @StephenMuecke is there some sort of documentation or commentary about this somewhere, as it seems like quiet a silly "by design" with providing no further control. Wouldn't the top down approach make more sense validate parent if parent good => validate child? – Heinrich Mar 06 '18 at 04:36
  • @Heinrich sorry my bad was testing different scenario. – Mike Mar 06 '18 at 04:37
  • @MikeMazmanyan haha all good. You killed that as I was reading it – Heinrich Mar 06 '18 at 04:39
  • 1
    I have seen an article from a MS team member documenting it in detail, but cannot find it. But Joao's answer [here](https://stackoverflow.com/questions/8153602/ivalidatableobject-validate-method-firing-when-dataannotations-fails/8168051) outlines the steps when using `IValidateObject` –  Mar 06 '18 at 04:39
  • @StephenMuecke awesome thanks for that I will take a look through it – Heinrich Mar 06 '18 at 04:42
  • For additional reasons to not use `IValidateObject`, scroll down to the _Exploring alternatives with IValidatableObject_ section in [The Complete Guide To Validation In ASP.NET MVC 3 - Part 2](https://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2) –  Mar 06 '18 at 04:47
  • _"my question however is not about the hierarchy it is about why the Validate function of parent doesn't get triggered if there is a validation error in the child"_ - your question is about hierarchy. As Stephen said, the behaviour you see is by design –  Mar 06 '18 at 05:17
  • @Heinrich updated my answer – Mike Mar 06 '18 at 06:01

1 Answers1

4

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.

Mike
  • 3,766
  • 3
  • 18
  • 32
  • OP question is about ASP.NET MVC Core which is using `ValidationVisitor` 100% – Mike Mar 06 '18 at 07:04
  • Confirmed. My mistake good sir, I swear I Googled before but I just found my mistake. Wishing you well –  Mar 06 '18 at 07:17
  • @MikeMazmanyan thanks for that, atleast there is some doc around at that I can see now. Unfortunately I don't have the luxury to upgrade to core 2. Side note it calls the IValidatableObject.Validate function of the child before performing the attribute validation of the parent *sigh*. – Heinrich Mar 06 '18 at 21:35