Skip to content

Commit

Permalink
fix(ValidateForm): OnValidSubmit runs before ValidateAsync (#2311)
Browse files Browse the repository at this point in the history
* fix: 修复异步 Validate 对结果有影响问题

* chore: bump version 7.12.1-beta04

* chore: 更新单词拼写错误

* test: 增加单元测试
  • Loading branch information
ArgoZhang committed Nov 2, 2023
1 parent 1b658d1 commit 2247aec
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>7.12.1-beta03</Version>
<Version>7.12.1-beta04</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
36 changes: 33 additions & 3 deletions src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,10 @@ private async Task ValidateAsync(IValidateComponent validator, ValidationContext
ValidateDataAnnotations(propertyValue, context, messages, pi);
if (messages.Count == 0)
{
_tcs = new();
// 自定义验证组件
await validator.ValidatePropertyAsync(propertyValue, context, messages);
_tcs.SetResult(!messages.Any());
}
}

Expand All @@ -456,6 +458,8 @@ internal void RegisterAsyncSubmitButton(ButtonBase button)
AsyncSubmitButtons.Add(button);
}

private TaskCompletionSource<bool>? _tcs;

private async Task OnValidSubmitForm(EditContext context)
{
var isAsync = AsyncSubmitButtons.Any();
Expand All @@ -467,10 +471,28 @@ private async Task OnValidSubmitForm(EditContext context)
{
await Task.Yield();
}
if (OnValidSubmit != null)

var valid = true;
// 由于可能有异步验证,需要等待异步验证结束
if (_tcs != null)
{
await OnValidSubmit(context);
valid = await _tcs.Task;
}
if (valid)
{
if (OnValidSubmit != null)
{
await OnValidSubmit(context);
}
}
else
{
if (OnInvalidSubmit != null)
{
await OnInvalidSubmit(context);
}
}

foreach (var b in AsyncSubmitButtons)
{
b.TriggerAsync(false);
Expand Down Expand Up @@ -520,13 +542,21 @@ public bool Validate()
/// <param name="value"></param>
public void NotifyFieldChanged(in FieldIdentifier fieldIdentifier, object? value)
{
ValueChagnedFields.AddOrUpdate(fieldIdentifier, key => value, (key, v) => value);
ValueChangedFields.AddOrUpdate(fieldIdentifier, key => value, (key, v) => value);
OnFieldValueChanged?.Invoke(fieldIdentifier.FieldName, value);
}

/// <summary>
/// 获取 当前表单值改变的属性集合
/// </summary>
/// <returns></returns>
public ConcurrentDictionary<FieldIdentifier, object?> ValueChangedFields { get; } = new();

/// <summary>
/// 获取 当前表单值改变的属性集合
/// </summary>
/// <returns></returns>
[Obsolete("已过期,单词拼写错误,请使用 ValueChangedFields,Please use ValueChangedFields instead. wrong typo")]
[ExcludeFromCodeCoverage]
public ConcurrentDictionary<FieldIdentifier, object?> ValueChagnedFields { get; } = new();
}
17 changes: 15 additions & 2 deletions test/UnitTest/Validators/ValidatorAsyncTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,33 @@ public class ValidatorAsyncTest : BootstrapBlazorTestBase
[Fact]
public async Task Validate_Ok()
{
var invalid = false;
var foo = new Foo() { Name = "Test" };
var cut = Context.RenderComponent<ValidateForm>(pb =>
{
pb.Add(a => a.Model, foo);
pb.Add(a => a.OnInvalidSubmit, context =>
{
invalid = true;
return Task.CompletedTask;
});
pb.AddChildContent<BootstrapInput<string>>(pb =>
{
pb.Add(a => a.Value, foo.Name);
pb.Add(a => a.ValueExpression, foo.GenerateValueExpression());
pb.Add(a => a.ValidateRules, new List<IValidator> { new MockValidator() });
});
pb.AddChildContent<Button>(pb =>
{
pb.Add(a => a.ButtonType, ButtonType.Submit);
});
});
var input = cut.Find("input");
await cut.InvokeAsync(() => input.Change(""));
await Task.Delay(300);
await cut.InvokeAsync(() => input.Change("1234"));

var button = cut.Find("button");
await cut.InvokeAsync(() => button.Click());
cut.WaitForState(() => invalid);
}

class MockValidator : ValidatorAsyncBase
Expand Down

0 comments on commit 2247aec

Please sign in to comment.