From b8c28d1be4ff154d5bff28605cb7d92a662ca13f Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 10 Sep 2024 10:33:47 +0800 Subject: [PATCH] feat(RequiredValidator): enhance the RequiredValidator class (#4247) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 更改内部变量名 * refactor: 移除公共属性 * feat: 增加多语言支持 * chore: 更新字典表 * test: 更新单元测试名称 * test: 更新单元测试 --- exclusion.dic | 1 + .../Components/Validate/ValidateBase.cs | 13 +++++++------ src/BootstrapBlazor/Locales/en.json | 2 +- src/BootstrapBlazor/Locales/zh.json | 2 +- src/BootstrapBlazor/Validators/FormItemValidator.cs | 12 ++++++------ src/BootstrapBlazor/Validators/RequiredValidator.cs | 10 ++++++++++ test/UnitTest/Components/ValidateFormTest.cs | 2 +- test/UnitTest/Validators/RequiredValidatorTest.cs | 8 +++++++- 8 files changed, 34 insertions(+), 16 deletions(-) diff --git a/exclusion.dic b/exclusion.dic index 7b757f30c16..5a8a66ab0b4 100644 --- a/exclusion.dic +++ b/exclusion.dic @@ -55,6 +55,7 @@ Github menuitem Segmenteds Responsives +resx tabset Splittings Foos diff --git a/src/BootstrapBlazor/Components/Validate/ValidateBase.cs b/src/BootstrapBlazor/Components/Validate/ValidateBase.cs index cd500126bd3..94eefa28f3f 100644 --- a/src/BootstrapBlazor/Components/Validate/ValidateBase.cs +++ b/src/BootstrapBlazor/Components/Validate/ValidateBase.cs @@ -227,7 +227,8 @@ protected virtual bool TryParseValueFromString(string value, [MaybeNullWhen(fals /// protected virtual bool IsRequired() => ShowRequired ?? FieldIdentifier ?.Model.GetType().GetPropertyByName(FieldIdentifier.Value.FieldName)!.GetCustomAttribute(true) != null - || (ValidateRules?.OfType().Select(i => i.Validator).OfType().Any() ?? false); + || (ValidateRules?.OfType().Select(i => i.IsRequired).Any() ?? false) + || (ValidateRules?.OfType().Any() ?? false); /// /// Gets a string that indicates the status of the field being edited. This will include @@ -295,7 +296,7 @@ protected override void OnParametersSet() if (ShowRequired is true) { - Rules.Add(new RequiredValidator() { ErrorMessage = RequiredErrorMessage ?? GetDefaultErrorMessage() }); + Rules.Add(new RequiredValidator() { ErrorMessage = RequiredErrorMessage ?? GetDefaultRequiredErrorMessage() }); } } @@ -321,12 +322,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender) } } - private string? _defaultErrorMessage; + private string? _defaultRequiredErrorMessage; - private string GetDefaultErrorMessage() + private string GetDefaultRequiredErrorMessage() { - _defaultErrorMessage ??= Localizer["DefaultErrorMessage"]; - return _defaultErrorMessage; + _defaultRequiredErrorMessage ??= Localizer["DefaultRequiredErrorMessage"]; + return _defaultRequiredErrorMessage; } #region Validation diff --git a/src/BootstrapBlazor/Locales/en.json b/src/BootstrapBlazor/Locales/en.json index 7aadc3c4f9b..3525d86a613 100644 --- a/src/BootstrapBlazor/Locales/en.json +++ b/src/BootstrapBlazor/Locales/en.json @@ -375,6 +375,6 @@ "LightModeText": "Light" }, "BootstrapBlazor.Components.ValidateBase": { - "DefaultErrorMessage": "{0} is required." + "DefaultRequiredErrorMessage": "{0} is required." } } diff --git a/src/BootstrapBlazor/Locales/zh.json b/src/BootstrapBlazor/Locales/zh.json index 5e80c58dc09..2e8c4069c71 100644 --- a/src/BootstrapBlazor/Locales/zh.json +++ b/src/BootstrapBlazor/Locales/zh.json @@ -375,6 +375,6 @@ "LightModeText": "明亮" }, "BootstrapBlazor.Components.ValidateBase": { - "DefaultErrorMessage": "{0}是必填项" + "DefaultRequiredErrorMessage": "{0}是必填项" } } diff --git a/src/BootstrapBlazor/Validators/FormItemValidator.cs b/src/BootstrapBlazor/Validators/FormItemValidator.cs index eb959db2d5b..b51266bd3e6 100644 --- a/src/BootstrapBlazor/Validators/FormItemValidator.cs +++ b/src/BootstrapBlazor/Validators/FormItemValidator.cs @@ -10,11 +10,6 @@ namespace BootstrapBlazor.Components; /// public class FormItemValidator(ValidationAttribute attribute) : ValidatorBase { - /// - /// 获得 ValidationAttribute 实例 - /// - public ValidationAttribute Validator { get; } = attribute; - /// /// 验证方法 /// @@ -23,10 +18,15 @@ public class FormItemValidator(ValidationAttribute attribute) : ValidatorBase /// ValidateResult 集合实例 public override void Validate(object? propertyValue, ValidationContext context, List results) { - var result = Validator.GetValidationResult(propertyValue, context); + var result = attribute.GetValidationResult(propertyValue, context); if (result != null) { results.Add(result); } } + + /// + /// 是否为 RequiredAttribute 标签特性 + /// + public bool IsRequired => attribute is RequiredAttribute; } diff --git a/src/BootstrapBlazor/Validators/RequiredValidator.cs b/src/BootstrapBlazor/Validators/RequiredValidator.cs index be45955e112..d9802a60aa6 100644 --- a/src/BootstrapBlazor/Validators/RequiredValidator.cs +++ b/src/BootstrapBlazor/Validators/RequiredValidator.cs @@ -4,6 +4,7 @@ using BootstrapBlazor.Localization.Json; using Microsoft.AspNetCore.Components.Forms; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; using System.Collections; using System.Globalization; @@ -43,6 +44,15 @@ public class RequiredValidator : ValidatorBase /// ValidateResult 集合实例 public override void Validate(object? propertyValue, ValidationContext context, List results) { + if (string.IsNullOrEmpty(ErrorMessage)) + { + var localizer = context.GetRequiredService>>(); + var l = localizer["DefaultRequiredErrorMessage"]; + if (!l.ResourceNotFound) + { + ErrorMessage = l.Value; + } + } var errorMessage = GetLocalizerErrorMessage(context, LocalizerFactory, Options); var memberNames = string.IsNullOrEmpty(context.MemberName) ? null : new string[] { context.MemberName }; if (propertyValue == null) diff --git a/test/UnitTest/Components/ValidateFormTest.cs b/test/UnitTest/Components/ValidateFormTest.cs index 294a9b857f8..a62544db42a 100644 --- a/test/UnitTest/Components/ValidateFormTest.cs +++ b/test/UnitTest/Components/ValidateFormTest.cs @@ -435,7 +435,7 @@ public async Task ValidateFromCode_Ok() } [Fact] - public async Task Validate_Servise_Ok() + public async Task Validate_Service_Ok() { var foo = new HasService(); var cut = Context.RenderComponent(pb => diff --git a/test/UnitTest/Validators/RequiredValidatorTest.cs b/test/UnitTest/Validators/RequiredValidatorTest.cs index 10807bb3cd1..110a76b33a4 100644 --- a/test/UnitTest/Validators/RequiredValidatorTest.cs +++ b/test/UnitTest/Validators/RequiredValidatorTest.cs @@ -35,7 +35,7 @@ public void AllowEmptyString_Ok() } [Fact] - public void EnnumerableValue_Ok() + public void EnumerableValue_Ok() { var foo = new Foo(); var validator = new RequiredValidator() @@ -75,5 +75,11 @@ public void Localizer_Ok() validator.Options.ResourceManagerStringLocalizerType = typeof(Foo); validator.Validate("v1", context, results); Assert.Empty(results); + + var provider = Context.Services.GetRequiredService(); + validator = new RequiredValidator(); + context = new ValidationContext(foo, provider, null); + validator.Validate(null, context, results); + Assert.Single(results); } }