diff --git a/src/BootstrapBlazor/Components/Validate/ValidateBase.cs b/src/BootstrapBlazor/Components/Validate/ValidateBase.cs index 704b384ab63..397df3641e3 100644 --- a/src/BootstrapBlazor/Components/Validate/ValidateBase.cs +++ b/src/BootstrapBlazor/Components/Validate/ValidateBase.cs @@ -474,11 +474,6 @@ public virtual void ToggleMessage(IEnumerable results) OnValidate(IsValid); } - if (!string.IsNullOrEmpty(ErrorMessage)) - { - ValidateForm?.AddValidationComponent(Id); - } - // 必须刷新一次 UI 保证状态正确 StateHasChanged(); } diff --git a/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs b/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs index 591331500ba..1dcf2fbae60 100644 --- a/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs +++ b/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs @@ -137,22 +137,6 @@ protected override void OnParametersSet() } } - /// - /// - /// - /// - /// - protected override async Task OnAfterRenderAsync(bool firstRender) - { - await base.OnAfterRenderAsync(firstRender); - - if (!ShowAllInvalidResult && _invalidComponents.Count > 0) - { - await InvokeVoidAsync("update", Id, _invalidComponents); - _invalidComponents.Clear(); - } - } - /// /// 添加数据验证组件到 EditForm 中 /// @@ -177,13 +161,14 @@ internal void AddValidator((string FieldName, Type ModelType) key, (FieldIdentif /// 错误描述信息,可为空,为空时查找资源文件 public void SetError(Expression> expression, string errorMessage) { - if (expression.Body is UnaryExpression unary && unary.Operand is MemberExpression mem) - { - InternalSetError(mem, errorMessage); - } - else if (expression.Body is MemberExpression exp) + switch (expression.Body) { - InternalSetError(exp, errorMessage); + case UnaryExpression { Operand: MemberExpression mem }: + InternalSetError(mem, errorMessage); + break; + case MemberExpression exp: + InternalSetError(exp, errorMessage); + break; } } @@ -633,9 +618,7 @@ private async Task OnInvalidSubmitForm(EditContext context) public bool Validate() { _invalid = true; - var ret = Validator.Validate() && !_invalid; - StateHasChanged(); - return ret; + return Validator.Validate() && !_invalid; } /// @@ -649,12 +632,12 @@ public void NotifyFieldChanged(in FieldIdentifier fieldIdentifier, object? value OnFieldValueChanged?.Invoke(fieldIdentifier.FieldName, value); } - private readonly List _invalidComponents = []; + //private readonly List _invalidComponents = []; - internal void AddValidationComponent(string id) - { - _invalidComponents.Add(id); - } + //internal void AddValidationComponent(string id) + //{ + // _invalidComponents.Add(id); + //} /// /// 获取 当前表单值改变的属性集合 diff --git a/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.js b/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.js index b130de9b290..0f9c0d5d7ea 100644 --- a/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.js +++ b/src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.js @@ -14,36 +14,6 @@ export function init(id) { }) } -export function update(id, invalidIds) { - const el = document.getElementById(id); - const items = [...el.querySelectorAll(".is-invalid")]; - const invalidElements = invalidIds.map(cId => { - const item = document.getElementById(cId); - let order = items.indexOf(item); - if (order === -1) { - const invalidEl = item.querySelector(".is-invalid"); - if (invalidEl) { - order = items.indexOf(invalidEl); - } - } - return { item, order }; - }); - invalidElements.sort((a, b) => b.order - a.order); - - const invalid = invalidElements.pop(); - if (invalid) { - const handler = setInterval(() => { - const tip = bootstrap.Tooltip.getInstance(invalid.item) - if (tip) { - clearInterval(handler); - if (!tip._isShown()) { - tip.show(); - } - } - }, 20); - } -} - export function dispose(id) { const el = document.getElementById(id) EventHandler.off(el, 'keydown') diff --git a/test/UnitTest/Components/ValidateFormTest.cs b/test/UnitTest/Components/ValidateFormTest.cs index 05b66440c37..045d0ec0ed0 100644 --- a/test/UnitTest/Components/ValidateFormTest.cs +++ b/test/UnitTest/Components/ValidateFormTest.cs @@ -524,7 +524,7 @@ public async Task ValidateFromCode_Ok() var form = cut.Instance; await cut.InvokeAsync(() => form.Validate()); - Assert.Contains("form-control invalid is-invalid", cut.Markup); + Assert.Contains("form-control valid is-invalid", cut.Markup); } [Fact]