Skip to content

Commit 620fafe

Browse files
authored
feat(ValidateForm): add ValidateAsync method (#7469)
* refactor: 重构代码 * feat(ValidateForm): add VadliateAsync method * chore: bump version 10.2.1-beta05 * doc: 更新示例 * test: 更新单元测试
1 parent f71821f commit 620fafe

9 files changed

Lines changed: 44 additions & 21 deletions

File tree

src/BootstrapBlazor.Server/Components/Samples/Cascaders.razor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -88,9 +88,9 @@ private EventItem[] GetEvents() =>
8888
}
8989
];
9090

91-
private void OnValidate()
91+
private async Task OnValidate()
9292
{
93-
ValidateForm1.Validate();
93+
await ValidateForm1.ValidateAsync();
9494
}
9595

9696
/// <summary>

src/BootstrapBlazor.Server/Components/Samples/ValidateForms.razor.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -249,10 +249,9 @@ private void OnValidateReset()
249249
[NotNull]
250250
private ValidateForm? ValidatorForm { get; set; }
251251

252-
private Task OnValidator()
252+
private async Task OnValidator()
253253
{
254-
ValidatorForm.Validate();
255-
return Task.CompletedTask;
254+
await ValidatorForm.ValidateAsync();
256255
}
257256

258257
[NotNull]

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.2.1-beta04</Version>
4+
<Version>10.2.1-beta05</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,8 @@ private async Task ClickEditButton(TItem item)
704704
private async Task ClickUpdateButtonCallback()
705705
{
706706
// 验证 InCell 模式下的表单
707-
if (!_inCellValidateForm.Validate())
707+
var valid = await _inCellValidateForm.ValidateAsync();
708+
if (!valid)
708709
{
709710
return;
710711
}

src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -47,10 +47,23 @@ protected override void OnInitialized()
4747
AddEditContextDataAnnotationsValidation();
4848
}
4949

50+
private TaskCompletionSource<bool>? _tcs;
5051
/// <summary>
5152
/// 手动验证表单方法
5253
/// </summary>
5354
/// <returns></returns>
55+
internal async Task<bool> ValidateAsync()
56+
{
57+
_tcs = new(false);
58+
var ret = CurrentEditContext.Validate();
59+
var valid = await _tcs.Task;
60+
return ret && valid;
61+
}
62+
63+
/// <summary>
64+
/// 手动验证表单方法
65+
/// </summary>
66+
[ExcludeFromCodeCoverage]
5467
internal bool Validate() => CurrentEditContext.Validate();
5568

5669
private void AddEditContextDataAnnotationsValidation()
@@ -93,6 +106,11 @@ private async Task ValidateModel(EditContext editContext, ValidationMessageStore
93106
}
94107
}
95108
editContext.NotifyValidationStateChanged();
109+
110+
if (_tcs != null)
111+
{
112+
_tcs.TrySetResult(validationResults.Count == 0);
113+
}
96114
}
97115

98116
private async Task ValidateField(EditContext editContext, ValidationMessageStore messages, FieldIdentifier field, IServiceProvider provider)

src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,18 @@ private async Task OnInvalidSubmitForm(EditContext context)
649649
private EditContext? _formlessEditContext;
650650

651651
/// <summary>
652-
/// 验证方法 用于代码调用触发表单验证
652+
/// 同步验证方法 用于代码调用触发表单验证(不支持某些组件的异步验证)
653653
/// </summary>
654-
/// <returns></returns>
654+
[Obsolete("已弃用,请使用 ValidateAsync 方法。Deprecated. Please use the ValidateAsync method.")]
655+
[ExcludeFromCodeCoverage]
655656
public bool Validate() => Validator.Validate();
656657

658+
/// <summary>
659+
/// 异步验证方法 用于代码调用触发表单验证(支持异步验证)
660+
/// </summary>
661+
/// <returns></returns>
662+
public Task<bool> ValidateAsync() => Validator.ValidateAsync();
663+
657664
/// <summary>
658665
/// 通知属性改变方法
659666
/// </summary>

test/UnitTest/Components/DateTimePickerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ await cut.InvokeAsync(() =>
12861286
Assert.Equal("02/15/2024", model.DateTime.Value.ToString("MM/dd/yyyy"));
12871287

12881288
// 录入非法数值 Validate 通过
1289-
var valid = cut.Instance.Validate();
1289+
var valid = await cut.InvokeAsync(cut.Instance.ValidateAsync);
12901290
Assert.True(valid);
12911291
}
12921292

test/UnitTest/Components/DateTimeRangeTest.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,12 @@ public async Task InValidateForm_Ok()
479479
cut.Contains("class=\"form-label\"");
480480

481481
// 验证
482-
var validate = true;
483-
await cut.InvokeAsync(() => validate = cut.Instance.Validate());
482+
var validate = await cut.InvokeAsync(cut.Instance.ValidateAsync);
484483
Assert.False(validate);
485484

486485
var range = cut.FindComponent<DateTimeRange>();
487486
var clear = range.Find(".is-clear");
488-
clear.Click();
487+
await cut.InvokeAsync(() => clear.Click());
489488

490489
range.Render(pb =>
491490
{
@@ -504,8 +503,7 @@ public async Task InValidateForm_Ok()
504503
{
505504
pb.Add(a => a.Model, foo);
506505
});
507-
validate = false;
508-
await cut.InvokeAsync(() => validate = cut.Instance.Validate());
506+
validate = await cut.InvokeAsync(cut.Instance.ValidateAsync);
509507
Assert.True(validate);
510508
}
511509

test/UnitTest/Components/InputNumberTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,15 @@ await cut.InvokeAsync(() =>
242242
});
243243
Assert.Equal(1, model.Count);
244244

245-
var valid = cut.Instance.Validate();
245+
var valid = await cut.InvokeAsync(cut.Instance.ValidateAsync);
246246
Assert.False(valid);
247247

248248
await cut.InvokeAsync(() =>
249249
{
250250
input.Change("t2");
251251
});
252252
Assert.Equal(1, model.Count);
253-
valid = cut.Instance.Validate();
253+
valid = await cut.InvokeAsync(cut.Instance.ValidateAsync);
254254
Assert.False(valid);
255255

256256
await cut.InvokeAsync(() =>
@@ -259,7 +259,7 @@ await cut.InvokeAsync(() =>
259259
});
260260
Assert.Equal(2, model.Count);
261261

262-
valid = cut.Instance.Validate();
262+
valid = await cut.InvokeAsync(cut.Instance.ValidateAsync);
263263
Assert.True(valid);
264264
}
265265

0 commit comments

Comments
 (0)