diff --git a/src/BootstrapBlazor/Components/Validate/ValidateBase.cs b/src/BootstrapBlazor/Components/Validate/ValidateBase.cs index 1aaf3e1d809..67d4fc8f490 100644 --- a/src/BootstrapBlazor/Components/Validate/ValidateBase.cs +++ b/src/BootstrapBlazor/Components/Validate/ValidateBase.cs @@ -356,11 +356,6 @@ public virtual bool IsComplexValue(object? value) => value != null && !value.GetType().IsAssignableTo(typeof(System.Collections.IEnumerable)) && value.GetType().IsClass; - /// - /// 获得/设置 是否执行了自定义异步验证 - /// - protected bool IsAsyncValidate { get; set; } - /// /// 属性验证方法 /// @@ -383,7 +378,6 @@ public async Task ValidatePropertyAsync(object? propertyValue, ValidationContext if (validator is IValidatorAsync v) { await v.ValidateAsync(propertyValue, context, results); - IsAsyncValidate = true; } else { @@ -404,7 +398,6 @@ public async Task ValidatePropertyAsync(object? propertyValue, ValidationContext if (validator is IValidatorAsync v) { await v.ValidateAsync(propertyValue, context, results); - IsAsyncValidate = true; } else { @@ -460,11 +453,8 @@ public virtual void ToggleMessage(IEnumerable results) OnValidate(IsValid); } - if (IsAsyncValidate) - { - IsAsyncValidate = false; - StateHasChanged(); - } + // 必须刷新一次 UI 保证状态正确 + StateHasChanged(); } private JSModule? ValidateModule { get; set; } diff --git a/test/UnitTest/Components/ValidateFormTest.cs b/test/UnitTest/Components/ValidateFormTest.cs index 2447eba97d6..41f50628e85 100644 --- a/test/UnitTest/Components/ValidateFormTest.cs +++ b/test/UnitTest/Components/ValidateFormTest.cs @@ -162,7 +162,7 @@ public void ShowLabel_Ok() } [Fact] - public void SetError_Ok() + public async Task SetError_Ok() { var foo = new Foo(); var dummy = new Dummy(); @@ -180,9 +180,9 @@ public void SetError_Ok() pb.Add(a => a.ValueExpression, Utility.GenerateValueExpression(dummy, "Value", typeof(DateTime?))); }); }); - cut.Instance.SetError("Name", "Test_SetError"); - cut.Instance.SetError("Test.Name", "Test_SetError"); - cut.Instance.SetError(f => f.Name, "Name_SetError"); + await cut.InvokeAsync(() => cut.Instance.SetError("Name", "Test_SetError")); + await cut.InvokeAsync(() => cut.Instance.SetError("Test.Name", "Test_SetError")); + await cut.InvokeAsync(() => cut.Instance.SetError(f => f.Name, "Name_SetError")); // 利用反射提高代码覆盖率 var method = typeof(ValidateForm).GetMethod("TryGetValidator", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); @@ -193,7 +193,7 @@ public void SetError_Ok() } [Fact] - public void SetError_UnaryExpression() + public async Task SetError_UnaryExpression() { var foo = new Foo(); var dummy = new Dummy(); @@ -211,13 +211,13 @@ public void SetError_UnaryExpression() pb.Add(a => a.ValueExpression, Utility.GenerateValueExpression(dummy, "Value", typeof(DateTime?))); }); }); - cut.Instance.SetError(f => f.Value, "Name_SetError"); + await cut.InvokeAsync(() => cut.Instance.SetError(f => f.Value, "Name_SetError")); // 利用反射提高代码覆盖率 var fieldInfo = cut.Instance.GetType().GetField("_validatorCache", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!; var cache = (ConcurrentDictionary<(string FieldName, Type ModelType), (FieldIdentifier FieldIdentifier, IValidateComponent ValidateComponent)>)fieldInfo.GetValue(cut.Instance)!; cache.Remove(("Value", typeof(Dummy)), out _); - cut.Instance.SetError(f => f.Value, "Name_SetError"); + await cut.InvokeAsync(() => cut.Instance.SetError(f => f.Value, "Name_SetError")); } [Fact]