From 959e7027b07b6c242f98438b901dc9a57904b097 Mon Sep 17 00:00:00 2001 From: Jeremy Skinner Date: Tue, 21 Nov 2017 19:40:25 +0000 Subject: [PATCH] Add ValidateComplexTypesIfChildValidationFails to ValidationVisitor - Allows more granular control of when model-level validation should run. Addresses #7014 --- .../ModelBinding/Validation/ValidationVisitor.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs index 4e721c5731..494a798537 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Validation/ValidationVisitor.cs @@ -29,6 +29,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Validation private ModelMetadata _metadata; private IValidationStrategy _strategy; + /// + /// Indicates whether validation of a complex type should be performed if validation fails for any of its children. The default behavior is false. + /// + public bool ValidateComplexTypesIfChildValidationFails { get; set; } + /// /// Creates a new . /// @@ -236,7 +241,8 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Validation } // Double-checking HasReachedMaxErrors just in case this model has no properties. - if (isValid && !_modelState.HasReachedMaxErrors) + // If validation has failed for any children, only validate the parent if ValidateComplexTypesIfChildValidationFails is true. + if ((isValid || ValidateComplexTypesIfChildValidationFails) && !_modelState.HasReachedMaxErrors) { isValid &= ValidateNode(); }