|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.ComponentModel.DataAnnotations; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +namespace Microsoft.AspNetCore.Components.Forms |
| 9 | +{ |
| 10 | + public class BlazorDataAnnotationsValidator : ComponentBase |
| 11 | + { |
| 12 | + private static readonly object ValidationContextValidatorKey = new object(); |
| 13 | + private ValidationMessageStore _validationMessageStore; |
| 14 | + |
| 15 | + [CascadingParameter] |
| 16 | + internal EditContext EditContext { get; set; } |
| 17 | + |
| 18 | + protected override void OnInitialized() |
| 19 | + { |
| 20 | + _validationMessageStore = new ValidationMessageStore(EditContext); |
| 21 | + |
| 22 | + // Perform object-level validation (starting from the root model) on request |
| 23 | + EditContext.OnValidationRequested += (sender, eventArgs) => |
| 24 | + { |
| 25 | + _validationMessageStore.Clear(); |
| 26 | + ValidateObject(EditContext.Model); |
| 27 | + EditContext.NotifyValidationStateChanged(); |
| 28 | + }; |
| 29 | + |
| 30 | + // Perform per-field validation on each field edit |
| 31 | + EditContext.OnFieldChanged += (sender, eventArgs) => |
| 32 | + ValidateField(EditContext, _validationMessageStore, eventArgs.FieldIdentifier); |
| 33 | + } |
| 34 | + |
| 35 | + internal void ValidateObject(object value) |
| 36 | + { |
| 37 | + if (value is null) |
| 38 | + { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (value is IEnumerable<object> enumerable) |
| 43 | + { |
| 44 | + var index = 0; |
| 45 | + foreach (var item in enumerable) |
| 46 | + { |
| 47 | + ValidateObject(item); |
| 48 | + index++; |
| 49 | + } |
| 50 | + |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + var validationResults = new List<ValidationResult>(); |
| 55 | + ValidateObject(value, validationResults); |
| 56 | + |
| 57 | + // Transfer results to the ValidationMessageStore |
| 58 | + foreach (var validationResult in validationResults) |
| 59 | + { |
| 60 | + if (!validationResult.MemberNames.Any()) |
| 61 | + { |
| 62 | + _validationMessageStore.Add(new FieldIdentifier(value, string.Empty), validationResult.ErrorMessage); |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + foreach (var memberName in validationResult.MemberNames) |
| 67 | + { |
| 68 | + var fieldIdentifier = new FieldIdentifier(value, memberName); |
| 69 | + _validationMessageStore.Add(fieldIdentifier, validationResult.ErrorMessage); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void ValidateObject(object value, List<ValidationResult> validationResults) |
| 75 | + { |
| 76 | + var validationContext = new ValidationContext(value); |
| 77 | + validationContext.Items.Add(ValidationContextValidatorKey, this); |
| 78 | + Validator.TryValidateObject(value, validationContext, validationResults, validateAllProperties: true); |
| 79 | + } |
| 80 | + |
| 81 | + internal static bool TryValidateRecursive(object value, ValidationContext validationContext) |
| 82 | + { |
| 83 | + if (validationContext.Items.TryGetValue(ValidationContextValidatorKey, out var result) && result is BlazorDataAnnotationsValidator validator) |
| 84 | + { |
| 85 | + validator.ValidateObject(value); |
| 86 | + |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + private static void ValidateField(EditContext editContext, ValidationMessageStore messages, in FieldIdentifier fieldIdentifier) |
| 94 | + { |
| 95 | + // DataAnnotations only validates public properties, so that's all we'll look for |
| 96 | + var propertyInfo = fieldIdentifier.Model.GetType().GetProperty(fieldIdentifier.FieldName); |
| 97 | + if (propertyInfo != null) |
| 98 | + { |
| 99 | + var propertyValue = propertyInfo.GetValue(fieldIdentifier.Model); |
| 100 | + var validationContext = new ValidationContext(fieldIdentifier.Model) |
| 101 | + { |
| 102 | + MemberName = propertyInfo.Name |
| 103 | + }; |
| 104 | + var results = new List<ValidationResult>(); |
| 105 | + |
| 106 | + Validator.TryValidateProperty(propertyValue, validationContext, results); |
| 107 | + messages.Clear(fieldIdentifier); |
| 108 | + messages.Add(fieldIdentifier, results.Select(result => result.ErrorMessage)); |
| 109 | + |
| 110 | + // We have to notify even if there were no messages before and are still no messages now, |
| 111 | + // because the "state" that changed might be the completion of some async validation task |
| 112 | + editContext.NotifyValidationStateChanged(); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments