diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index e73e8b62633..3f5f3f23638 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 9.2.1-beta02 + 9.2.1-beta03 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); + } } diff --git a/src/BootstrapBlazor/Validators/RequiredValidator.cs b/src/BootstrapBlazor/Validators/RequiredValidator.cs index a0d60a7e753..063b8f045bf 100644 --- a/src/BootstrapBlazor/Validators/RequiredValidator.cs +++ b/src/BootstrapBlazor/Validators/RequiredValidator.cs @@ -53,17 +53,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(GetValidationResult(context)); } else if (propertyValue is string val) { if (!AllowEmptyString && val == string.Empty) { - results.Add(new ValidationResult(errorMessage, memberNames)); + results.Add(GetValidationResult(context)); } } else if (propertyValue is IEnumerable v) @@ -72,15 +70,21 @@ public override void Validate(object? propertyValue, ValidationContext context, var valid = enumerator.MoveNext(); if (!valid) { - results.Add(new ValidationResult(errorMessage, memberNames)); + results.Add(GetValidationResult(context)); } } else if (propertyValue is DateTimeRangeValue dv && dv is { NullStart: null, NullEnd: null }) { - results.Add(new ValidationResult(errorMessage, memberNames)); + results.Add(GetValidationResult(context)); } } + private ValidationResult GetValidationResult(ValidationContext context) + { + var errorMessage = GetLocalizerErrorMessage(context, LocalizerFactory, Options); + return context.GetValidationResult(errorMessage); + } + /// /// 获得当前验证规则资源文件中 Key 格式 /// 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);