Add ValidateComplexTypesIfChildValidationFails to ValidationVisitor

- Allows more granular control of when model-level validation should run.

Addresses #7014
This commit is contained in:
Jeremy Skinner 2017-11-21 19:40:25 +00:00 committed by Ryan Nowak
parent 24d245c16c
commit 959e7027b0
1 changed files with 7 additions and 1 deletions

View File

@ -29,6 +29,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Validation
private ModelMetadata _metadata;
private IValidationStrategy _strategy;
/// <summary>
/// Indicates whether validation of a complex type should be performed if validation fails for any of its children. The default behavior is false.
/// </summary>
public bool ValidateComplexTypesIfChildValidationFails { get; set; }
/// <summary>
/// Creates a new <see cref="ValidationVisitor"/>.
/// </summary>
@ -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();
}