Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>10.2.1-beta02</Version>
<Version>10.2.1-beta03</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
25 changes: 4 additions & 21 deletions src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ private bool TryGetValidator(Type modelType, string fieldName, out IValidateComp
/// <param name="results"></param>
internal async Task ValidateObject(ValidationContext context, List<ValidationResult> results)
{
_tcs = new TaskCompletionSource<bool>();
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a potential concurrency issue with the TaskCompletionSource field. If ValidateObject is called multiple times before the previous validation completes (e.g., rapid form submissions), the _tcs field could be overwritten, causing the previous await in OnValidSubmitForm to wait on the wrong task. Consider using a local variable instead of a field, or implementing proper synchronization to prevent concurrent validations.

Copilot uses AI. Check for mistakes.
_validateResults.Clear();

if (ValidateAllProperties)
Expand Down Expand Up @@ -343,14 +344,9 @@ internal async Task ValidateObject(ValidationContext context, List<ValidationRes
{
await validator.ToggleMessage(messages);
}

// 确保 _invalid 在没有验证组件时被正确设置
// Ensure _invalid is properly set when there are no validation components
if (_validatorCache.IsEmpty)
{
_invalid = results.Count > 0;
}
}

_tcs.TrySetResult(results.Count == 0);
}

/// <summary>
Expand Down Expand Up @@ -539,19 +535,14 @@ private async Task ValidateAsync(IValidateComponent validator, ValidationContext
}
ValidateDataAnnotations(propertyValue, context, messages, pi);
}

_tcs = new TaskCompletionSource<bool>();
_tcs.TrySetResult(messages.Count == 0);
}
else
{
ValidateDataAnnotations(propertyValue, context, messages, pi);
if (messages.Count == 0)
{
// 自定义验证组件
_tcs = new TaskCompletionSource<bool>();
await validator.ValidatePropertyAsync(propertyValue, context, messages);
_tcs.TrySetResult(messages.Count == 0);
}

if (messages.Count == 0)
Expand All @@ -574,12 +565,8 @@ private async Task ValidateAsync(IValidateComponent validator, ValidationContext
}
}
}

_invalid = messages.Count > 0;
}

private bool _invalid = false;

private List<ButtonBase> AsyncSubmitButtons { get; } = [];

/// <summary>
Expand Down Expand Up @@ -665,11 +652,7 @@ private async Task OnInvalidSubmitForm(EditContext context)
/// 验证方法 用于代码调用触发表单验证
/// </summary>
/// <returns></returns>
public bool Validate()
{
_invalid = true;
return Validator.Validate() && !_invalid;
}
public bool Validate() => Validator.Validate();

/// <summary>
/// 通知属性改变方法
Expand Down
Loading