From 1b25367f66238650066c4242d9401cb9a1420c9e Mon Sep 17 00:00:00 2001 From: maomao_zhen Date: Mon, 30 Dec 2024 11:41:29 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=BF=85=E5=A1=AB=E9=AA=8C=E8=AF=81=E3=80=90RequiredA?= =?UTF-8?q?ttribute=E3=80=91=E9=80=9A=E8=BF=87=E5=90=8E=EF=BC=88=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=20ValidationResult=20=E9=9B=86=E5=90=88=E4=B8=BA?= =?UTF-8?q?=E9=9B=B6=E6=97=B6=EF=BC=89=EF=BC=8CRequiredValidator=20?= =?UTF-8?q?=E5=9C=A8=E9=AA=8C=E8=AF=81=E6=9C=AA=E5=87=BA=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=EF=BC=88=E7=BB=93=E6=9E=9C=E5=BF=85=E5=AE=9A?= =?UTF-8?q?=E4=B8=BA=E9=80=9A=E8=BF=87=EF=BC=89=E6=9C=AC=E5=9C=B0=E5=8C=96?= =?UTF-8?q?=E5=8C=85=E5=90=AB=E8=87=AA=E5=AE=9A=E4=B9=89=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E7=9A=84=20ErrorMessage=20=E4=B8=94=E5=85=B6=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E4=B8=AD=E6=9C=89=E4=B8=8D=E7=AC=A6=E5=90=88=20string.Format?= =?UTF-8?q?=20=E6=96=B9=E6=B3=95=E8=A6=81=E6=B1=82=E7=9A=84=E5=86=85?= =?UTF-8?q?=E5=AE=B9=EF=BC=88=E5=A6=82=EF=BC=9AErrorMessage=20=3D=20$"?= =?UTF-8?q?=E5=BD=93=E5=89=8D=20{{{nameof(MenuType)}}}=20=E7=9A=84?= =?UTF-8?q?=E5=80=BC=E5=BF=85=E9=A1=BB=E8=A6=81=E8=AE=BE=E7=BD=AE=20{{0}}?= =?UTF-8?q?=20=E3=80=82"=EF=BC=89=E6=97=B6=E5=AF=BC=E8=87=B4=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E5=B4=A9=E6=BA=83=E9=97=AE=E9=A2=98=E3=80=82=20?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E9=AA=8C=E8=AF=81=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=BC=9A=E6=AD=A3=E7=A1=AE=E5=A4=84=E7=90=86=20{{{nameof(MenuT?= =?UTF-8?q?ype)}}}=20=E8=87=AA=E5=AE=9A=E4=B9=89=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Validators/RequiredValidator.cs | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/BootstrapBlazor/Validators/RequiredValidator.cs b/src/BootstrapBlazor/Validators/RequiredValidator.cs index a0d60a7e753..6433626145e 100644 --- a/src/BootstrapBlazor/Validators/RequiredValidator.cs +++ b/src/BootstrapBlazor/Validators/RequiredValidator.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Localization; using System.Collections; using System.Globalization; +using System.Security.AccessControl; namespace BootstrapBlazor.Components; @@ -53,17 +54,15 @@ public override void Validate(object? propertyValue, ValidationContext context, ErrorMessage = l.Value; } } - var errorMessage = GetLocalizerErrorMessage(context, LocalizerFactory, Options); - var memberNames = string.IsNullOrEmpty(context.MemberName) ? null : new string[] { context.MemberName }; if (propertyValue == null) { - results.Add(new ValidationResult(errorMessage, memberNames)); + results.Add(CreateValidationResult(context)); } else if (propertyValue is string val) { if (!AllowEmptyString && val == string.Empty) { - results.Add(new ValidationResult(errorMessage, memberNames)); + results.Add(CreateValidationResult(context)); } } else if (propertyValue is IEnumerable v) @@ -72,14 +71,26 @@ public override void Validate(object? propertyValue, ValidationContext context, var valid = enumerator.MoveNext(); if (!valid) { - results.Add(new ValidationResult(errorMessage, memberNames)); + results.Add(CreateValidationResult(context)); } } else if (propertyValue is DateTimeRangeValue dv && dv is { NullStart: null, NullEnd: null }) { - results.Add(new ValidationResult(errorMessage, memberNames)); + results.Add(CreateValidationResult(context)); } } + /// + /// 生成错误提示信息。 + /// + /// + /// + private ValidationResult CreateValidationResult(ValidationContext context) + { + var errorMessage = GetLocalizerErrorMessage(context, LocalizerFactory, Options); + var memberNames = string.IsNullOrEmpty(context.MemberName) ? null : new string[] { context.MemberName }; + return new ValidationResult(errorMessage, memberNames); + } + /// /// 获得当前验证规则资源文件中 Key 格式 From 2a15b8820023389f08eb86aa87bd82579936047a Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 31 Dec 2024 13:33:03 +0800 Subject: [PATCH 2/6] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=20GetValidat?= =?UTF-8?q?ionResult=20=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/ValidateContextExtensions.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/BootstrapBlazor/Extensions/ValidateContextExtensions.cs b/src/BootstrapBlazor/Extensions/ValidateContextExtensions.cs index 3e36021fc59..2c6e01eea92 100644 --- a/src/BootstrapBlazor/Extensions/ValidateContextExtensions.cs +++ b/src/BootstrapBlazor/Extensions/ValidateContextExtensions.cs @@ -30,4 +30,16 @@ public static class ValidationContextExtensions } return ret; } + + /// + /// 获得 实例 + /// + /// + /// + /// + public static ValidationResult GetValidationResult(this ValidationContext context, string? errorMessage) + { + var memberNames = string.IsNullOrEmpty(context.MemberName) ? null : new string[] { context.MemberName }; + return new ValidationResult(errorMessage, memberNames); + } } From 3a5731c26ada7ad71d6b991cf60dc5ec992840e2 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 31 Dec 2024 13:33:17 +0800 Subject: [PATCH 3/6] =?UTF-8?q?refactor:=20=E5=88=A9=E7=94=A8=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E6=96=B9=E6=B3=95=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Validators/RequiredValidator.cs | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/BootstrapBlazor/Validators/RequiredValidator.cs b/src/BootstrapBlazor/Validators/RequiredValidator.cs index 6433626145e..d643aadc81a 100644 --- a/src/BootstrapBlazor/Validators/RequiredValidator.cs +++ b/src/BootstrapBlazor/Validators/RequiredValidator.cs @@ -56,13 +56,13 @@ public override void Validate(object? propertyValue, ValidationContext context, } if (propertyValue == null) { - results.Add(CreateValidationResult(context)); + results.Add(GetValidationResult(context)); } else if (propertyValue is string val) { if (!AllowEmptyString && val == string.Empty) { - results.Add(CreateValidationResult(context)); + results.Add(GetValidationResult(context)); } } else if (propertyValue is IEnumerable v) @@ -71,27 +71,21 @@ public override void Validate(object? propertyValue, ValidationContext context, var valid = enumerator.MoveNext(); if (!valid) { - results.Add(CreateValidationResult(context)); + results.Add(GetValidationResult(context)); } } else if (propertyValue is DateTimeRangeValue dv && dv is { NullStart: null, NullEnd: null }) { - results.Add(CreateValidationResult(context)); + results.Add(GetValidationResult(context)); } } - /// - /// 生成错误提示信息。 - /// - /// - /// - private ValidationResult CreateValidationResult(ValidationContext context) + + private ValidationResult GetValidationResult(ValidationContext context) { var errorMessage = GetLocalizerErrorMessage(context, LocalizerFactory, Options); - var memberNames = string.IsNullOrEmpty(context.MemberName) ? null : new string[] { context.MemberName }; - return new ValidationResult(errorMessage, memberNames); + return context.GetValidationResult(errorMessage); } - /// /// 获得当前验证规则资源文件中 Key 格式 /// From 036631ab65114317d325a1173abf39d203a558bb Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 31 Dec 2024 13:33:47 +0800 Subject: [PATCH 4/6] chore: bump version 9.2.1-beta03 --- 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 047f0af70a4..3f5f3f23638 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 9.2.0 + 9.2.1-beta03 From 7fdb50127feb38b449c9bc9caf678ed4900cc0ad Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 31 Dec 2024 13:34:50 +0800 Subject: [PATCH 5/6] =?UTF-8?q?refactor:=20=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E7=A9=BA=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Validators/RequiredValidator.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/BootstrapBlazor/Validators/RequiredValidator.cs b/src/BootstrapBlazor/Validators/RequiredValidator.cs index d643aadc81a..063b8f045bf 100644 --- a/src/BootstrapBlazor/Validators/RequiredValidator.cs +++ b/src/BootstrapBlazor/Validators/RequiredValidator.cs @@ -8,7 +8,6 @@ using Microsoft.Extensions.Localization; using System.Collections; using System.Globalization; -using System.Security.AccessControl; namespace BootstrapBlazor.Components; From 2a3b5594d27a9fd2bc1009c61ceef19058cae5d3 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 31 Dec 2024 13:46:41 +0800 Subject: [PATCH 6/6] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Validators/RequiredValidatorTest.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/UnitTest/Validators/RequiredValidatorTest.cs b/test/UnitTest/Validators/RequiredValidatorTest.cs index 621fe5ddea5..4a86aeaebef 100644 --- a/test/UnitTest/Validators/RequiredValidatorTest.cs +++ b/test/UnitTest/Validators/RequiredValidatorTest.cs @@ -37,6 +37,7 @@ public void AllowEmptyString_Ok() [Fact] public void EnumerableValue_Ok() { + int[] value = [1, 2]; var foo = new Foo(); var validator = new RequiredValidator() { @@ -45,7 +46,7 @@ public void EnumerableValue_Ok() }; var context = new ValidationContext(foo); var results = new List(); - validator.Validate(new int[] { 1, 2 }, context, results); + validator.Validate(value, context, results); Assert.Empty(results); validator.Validate(Array.Empty(), context, results); @@ -76,6 +77,10 @@ public void Localizer_Ok() validator.Validate("v1", context, results); Assert.Empty(results); + validator.Validate("", context, results); + Assert.Single(results); + + results.Clear(); var provider = Context.Services.GetRequiredService(); validator = new RequiredValidator(); context = new ValidationContext(foo, provider, null);