-
-
Notifications
You must be signed in to change notification settings - Fork 382
feat(ValidateForm): add ValidateAsync method #7469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||||||||||||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||||||||||||||||
| // Licensed to the .NET Foundation under one or more agreements. | ||||||||||||||||||||
| // The .NET Foundation licenses this file to you under the Apache 2.0 License | ||||||||||||||||||||
| // See the LICENSE file in the project root for more information. | ||||||||||||||||||||
| // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone | ||||||||||||||||||||
|
|
@@ -47,10 +47,23 @@ protected override void OnInitialized() | |||||||||||||||||||
| AddEditContextDataAnnotationsValidation(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| private TaskCompletionSource<bool>? _tcs; | ||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||
| /// 手动验证表单方法 | ||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||
| /// <returns></returns> | ||||||||||||||||||||
| internal async Task<bool> ValidateAsync() | ||||||||||||||||||||
| { | ||||||||||||||||||||
| _tcs = new(false); | ||||||||||||||||||||
|
||||||||||||||||||||
| _tcs = new(false); | |
| _tcs = new(System.Threading.Tasks.TaskCreationOptions.RunContinuationsAsynchronously); |
Copilot
AI
Jan 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The _tcs field is not thread-safe and can cause race conditions. If ValidateAsync() is called multiple times concurrently or before a previous validation completes, the field will be overwritten and the previous caller will await a task that never completes or receives the wrong result. Consider using a local variable for the TaskCompletionSource instead, or implement proper synchronization to prevent concurrent validations.
Copilot
AI
Jan 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The _tcs field check and result setting should handle the case where the field might be null. While the current implementation sets _tcs in ValidateAsync(), the ValidateModel method can also be called from OnValidationRequested event handler (line 83) where _tcs would be null. This creates inconsistent behavior - the TaskCompletionSource is only set when validation is triggered via ValidateAsync(), not when triggered by the EditContext's validation events.
| if (_tcs != null) | |
| { | |
| _tcs.TrySetResult(validationResults.Count == 0); | |
| // Safely complete any pending TaskCompletionSource associated with this validation. | |
| var tcs = _tcs; | |
| if (tcs != null) | |
| { | |
| tcs.TrySetResult(validationResults.Count == 0); | |
| _tcs = null; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -649,11 +649,18 @@ private async Task OnInvalidSubmitForm(EditContext context) | |||||
| private EditContext? _formlessEditContext; | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// 验证方法 用于代码调用触发表单验证 | ||||||
| /// 同步验证方法 用于代码调用触发表单验证(不支持某些组件的异步验证) | ||||||
| /// </summary> | ||||||
| /// <returns></returns> | ||||||
| [Obsolete("已弃用,请使用 ValidateAsync 方法。Deprecated. Please use the ValidateAsync method.")] | ||||||
| [ExcludeFromCodeCoverage] | ||||||
| public bool Validate() => Validator.Validate(); | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// 异步验证方法 用于代码调用触发表单验证(支持异步验证) | ||||||
| /// </summary> | ||||||
| /// <returns></returns> | ||||||
|
||||||
| /// <returns></returns> | |
| /// <returns>A Task that resolves to true if validation succeeds; otherwise, false.</returns> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The XML documentation summary "手动验证表单方法" (Manual form validation method) is insufficient. It should describe what the method returns and clarify that it performs asynchronous validation. Consider adding a returns tag explaining that it returns a Task that resolves to true if validation succeeds, false otherwise.