From 2b421704ce2c8da5b8d1703033abe1be878a2d98 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 3 Aug 2025 11:53:09 +0800 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...zorEditContextDataAnnotationsExtensions.cs | 70 ------------------- 1 file changed, 70 deletions(-) delete mode 100644 src/BootstrapBlazor/Extensions/BootstrapBlazorEditContextDataAnnotationsExtensions.cs diff --git a/src/BootstrapBlazor/Extensions/BootstrapBlazorEditContextDataAnnotationsExtensions.cs b/src/BootstrapBlazor/Extensions/BootstrapBlazorEditContextDataAnnotationsExtensions.cs deleted file mode 100644 index 07bda88791f..00000000000 --- a/src/BootstrapBlazor/Extensions/BootstrapBlazorEditContextDataAnnotationsExtensions.cs +++ /dev/null @@ -1,70 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the Apache 2.0 License -// See the LICENSE file in the project root for more information. -// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone - -using Microsoft.AspNetCore.Components.Forms; - -namespace BootstrapBlazor.Components; - -/// -/// BootstrapBlazorEditContextDataAnnotationsExtensions 扩展操作类 -/// -internal static class BootstrapBlazorEditContextDataAnnotationsExtensions -{ - /// - /// 添加数据合规检查 - /// - /// The . - /// - /// - public static EditContext AddEditContextDataAnnotationsValidation(this EditContext editContext, ValidateForm editForm, IServiceProvider provider) - { - var messages = new ValidationMessageStore(editContext); - - editContext.OnValidationRequested += async (sender, eventArgs) => await editForm.ValidateModel(sender as EditContext, messages, provider); - editContext.OnFieldChanged += async (sender, eventArgs) => await editForm.ValidateField(editContext, messages, eventArgs, provider); - return editContext; - } - - private static async Task ValidateModel(this ValidateForm editForm, EditContext? editContext, ValidationMessageStore messages, IServiceProvider provider) - { - if (editContext != null) - { - var validationContext = new ValidationContext(editContext.Model, provider, null); - var validationResults = new List(); - await editForm.ValidateObject(validationContext, validationResults); - - messages.Clear(); - foreach (var validationResult in validationResults.Where(v => !string.IsNullOrEmpty(v.ErrorMessage))) - { - foreach (var memberName in validationResult.MemberNames) - { - if (!string.IsNullOrEmpty(memberName)) - { - messages.Add(editContext.Field(memberName), validationResult.ErrorMessage!); - } - } - } - editContext.NotifyValidationStateChanged(); - } - } - - private static async Task ValidateField(this ValidateForm editForm, EditContext editContext, ValidationMessageStore messages, FieldChangedEventArgs args, IServiceProvider provider) - { - // 获取验证消息 - var validationResults = new List(); - var validationContext = new ValidationContext(args.FieldIdentifier.Model, provider, null) - { - MemberName = args.FieldIdentifier.FieldName, - DisplayName = args.FieldIdentifier.GetDisplayName() - }; - - await editForm.ValidateFieldAsync(validationContext, validationResults); - - messages.Clear(args.FieldIdentifier); - messages.Add(args.FieldIdentifier, validationResults.Where(v => !string.IsNullOrEmpty(v.ErrorMessage)).Select(result => result.ErrorMessage!)); - - editContext.NotifyValidationStateChanged(); - } -} From 9baa3d4c4194346876fed11ce081e6d8134f3f3d Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 3 Aug 2025 11:53:21 +0800 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=20IDisposabl?= =?UTF-8?q?e=20=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...BootstrapBlazorDataAnnotationsValidator.cs | 90 +++++++++++++++++-- 1 file changed, 85 insertions(+), 5 deletions(-) diff --git a/src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs b/src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs index 8ff9f18b263..5bf22e97ebe 100644 --- a/src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs +++ b/src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs @@ -10,7 +10,7 @@ namespace BootstrapBlazor.Components; /// /// BootstrapBlazorDataAnnotationsValidator 验证组件 /// -public class BootstrapBlazorDataAnnotationsValidator : ComponentBase +public class BootstrapBlazorDataAnnotationsValidator : ComponentBase, IDisposable { /// /// 获得/设置 当前编辑数据上下文 @@ -23,12 +23,16 @@ public class BootstrapBlazorDataAnnotationsValidator : ComponentBase /// 获得/设置 当前编辑窗体上下文 /// [CascadingParameter] + [NotNull] private ValidateForm? ValidateForm { get; set; } [Inject] [NotNull] private IServiceProvider? Provider { get; set; } + [NotNull] + private ValidationMessageStore? _message = null; + /// /// 初始化方法 /// @@ -36,12 +40,11 @@ protected override void OnInitialized() { if (ValidateForm == null) { - throw new InvalidOperationException($"{nameof(Components.BootstrapBlazorDataAnnotationsValidator)} requires a cascading " + - $"parameter of type {nameof(Components.ValidateForm)}. For example, you can use {nameof(Components.BootstrapBlazorDataAnnotationsValidator)} " + - $"inside an {nameof(Components.ValidateForm)}."); + throw new InvalidOperationException($"{nameof(BootstrapBlazorDataAnnotationsValidator)} requires a cascading parameter of type {nameof(Components.ValidateForm)}. For example, you can use {nameof(BootstrapBlazorDataAnnotationsValidator)} inside an {nameof(Components.ValidateForm)}."); } - CurrentEditContext.AddEditContextDataAnnotationsValidation(ValidateForm, Provider); + _message = new ValidationMessageStore(CurrentEditContext); + AddEditContextDataAnnotationsValidation(); } /// @@ -49,4 +52,81 @@ protected override void OnInitialized() /// /// internal bool Validate() => CurrentEditContext.Validate(); + + private void AddEditContextDataAnnotationsValidation() + { + CurrentEditContext.OnValidationRequested += OnValidationRequested; + CurrentEditContext.OnFieldChanged += OnFieldChanged; + } + + private void RemoveEditContextDataAnnotationsValidation() + { + CurrentEditContext.OnValidationRequested -= OnValidationRequested; + CurrentEditContext.OnFieldChanged -= OnFieldChanged; + } + + private void OnValidationRequested(object? sender, ValidationRequestedEventArgs args) + { + _ = ValidateModel(CurrentEditContext, _message, Provider); + } + + private void OnFieldChanged(object? sender, FieldChangedEventArgs args) + { + _ = ValidateField(CurrentEditContext, _message, args, Provider); + } + + private async Task ValidateModel(EditContext editContext, ValidationMessageStore messages, IServiceProvider provider) + { + var validationContext = new ValidationContext(editContext.Model, provider, null); + var validationResults = new List(); + await ValidateForm.ValidateObject(validationContext, validationResults); + + messages.Clear(); + foreach (var validationResult in validationResults.Where(v => !string.IsNullOrEmpty(v.ErrorMessage))) + { + foreach (var memberName in validationResult.MemberNames) + { + if (!string.IsNullOrEmpty(memberName)) + { + messages.Add(editContext.Field(memberName), validationResult.ErrorMessage!); + } + } + } + editContext.NotifyValidationStateChanged(); + } + + private async Task ValidateField(EditContext editContext, ValidationMessageStore messages, FieldChangedEventArgs args, IServiceProvider provider) + { + // 获取验证消息 + var validationResults = new List(); + var validationContext = new ValidationContext(args.FieldIdentifier.Model, provider, null) + { + MemberName = args.FieldIdentifier.FieldName, + DisplayName = args.FieldIdentifier.GetDisplayName() + }; + + await ValidateForm.ValidateFieldAsync(validationContext, validationResults); + + messages.Clear(args.FieldIdentifier); + messages.Add(args.FieldIdentifier, validationResults.Where(v => !string.IsNullOrEmpty(v.ErrorMessage)).Select(result => result.ErrorMessage!)); + + editContext.NotifyValidationStateChanged(); + } + + private void Dispose(bool disposing) + { + if (disposing) + { + RemoveEditContextDataAnnotationsValidation(); + } + } + + /// + /// + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } } From 96aa6038887ce2dd15e268d4b4b60da2d0d56c4c Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 3 Aug 2025 11:53:50 +0800 Subject: [PATCH 3/4] chore: bump version 9.9.1-beta04 --- src/BootstrapBlazor/BootstrapBlazor.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 2b1b02d2118..c4d91628639 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@  - 9.9.1-beta03 + 9.9.1-beta04 From 77f83125641f164b9c43a27cf27ca42a4903610e Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 3 Aug 2025 12:12:40 +0800 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20=E7=B2=BE=E7=AE=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BootstrapBlazorDataAnnotationsValidator.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs b/src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs index 5bf22e97ebe..e75d5306d44 100644 --- a/src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs +++ b/src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs @@ -72,7 +72,7 @@ private void OnValidationRequested(object? sender, ValidationRequestedEventArgs private void OnFieldChanged(object? sender, FieldChangedEventArgs args) { - _ = ValidateField(CurrentEditContext, _message, args, Provider); + _ = ValidateField(CurrentEditContext, _message, args.FieldIdentifier, Provider); } private async Task ValidateModel(EditContext editContext, ValidationMessageStore messages, IServiceProvider provider) @@ -95,20 +95,20 @@ private async Task ValidateModel(EditContext editContext, ValidationMessageStore editContext.NotifyValidationStateChanged(); } - private async Task ValidateField(EditContext editContext, ValidationMessageStore messages, FieldChangedEventArgs args, IServiceProvider provider) + private async Task ValidateField(EditContext editContext, ValidationMessageStore messages, FieldIdentifier field, IServiceProvider provider) { // 获取验证消息 var validationResults = new List(); - var validationContext = new ValidationContext(args.FieldIdentifier.Model, provider, null) + var validationContext = new ValidationContext(field.Model, provider, null) { - MemberName = args.FieldIdentifier.FieldName, - DisplayName = args.FieldIdentifier.GetDisplayName() + MemberName = field.FieldName, + DisplayName = field.GetDisplayName() }; await ValidateForm.ValidateFieldAsync(validationContext, validationResults); - messages.Clear(args.FieldIdentifier); - messages.Add(args.FieldIdentifier, validationResults.Where(v => !string.IsNullOrEmpty(v.ErrorMessage)).Select(result => result.ErrorMessage!)); + messages.Clear(field); + messages.Add(field, validationResults.Where(v => !string.IsNullOrEmpty(v.ErrorMessage)).Select(result => result.ErrorMessage!)); editContext.NotifyValidationStateChanged(); }