Add ValidateComplexTypesIfChildValidationFails to ValidationVisitor
- Allows more granular control of when model-level validation should run. Addresses #7014
This commit is contained in:
parent
24d245c16c
commit
959e7027b0
|
|
@ -29,6 +29,11 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Validation
|
||||||
private ModelMetadata _metadata;
|
private ModelMetadata _metadata;
|
||||||
private IValidationStrategy _strategy;
|
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>
|
/// <summary>
|
||||||
/// Creates a new <see cref="ValidationVisitor"/>.
|
/// Creates a new <see cref="ValidationVisitor"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -236,7 +241,8 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Validation
|
||||||
}
|
}
|
||||||
|
|
||||||
// Double-checking HasReachedMaxErrors just in case this model has no properties.
|
// 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();
|
isValid &= ValidateNode();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue