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
5 changes: 0 additions & 5 deletions src/BootstrapBlazor/Components/Validate/ValidateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,6 @@ public virtual void ToggleMessage(IEnumerable<ValidationResult> results)
OnValidate(IsValid);
}

if (!string.IsNullOrEmpty(ErrorMessage))
{
ValidateForm?.AddValidationComponent(Id);
}

// 必须刷新一次 UI 保证状态正确
StateHasChanged();
}
Expand Down
43 changes: 13 additions & 30 deletions src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,6 @@ protected override void OnParametersSet()
}
}

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="firstRender"></param>
/// <returns></returns>
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);

if (!ShowAllInvalidResult && _invalidComponents.Count > 0)
{
await InvokeVoidAsync("update", Id, _invalidComponents);
_invalidComponents.Clear();
}
}

/// <summary>
/// 添加数据验证组件到 EditForm 中
/// </summary>
Expand All @@ -177,13 +161,14 @@ internal void AddValidator((string FieldName, Type ModelType) key, (FieldIdentif
/// <param name="errorMessage">错误描述信息,可为空,为空时查找资源文件</param>
public void SetError<TModel>(Expression<Func<TModel, object?>> 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;
}
}

Expand Down Expand Up @@ -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;
}

/// <summary>
Expand All @@ -649,12 +632,12 @@ public void NotifyFieldChanged(in FieldIdentifier fieldIdentifier, object? value
OnFieldValueChanged?.Invoke(fieldIdentifier.FieldName, value);
}

private readonly List<string> _invalidComponents = [];
//private readonly List<string> _invalidComponents = [];

internal void AddValidationComponent(string id)
{
_invalidComponents.Add(id);
}
//internal void AddValidationComponent(string id)
//{
// _invalidComponents.Add(id);
//}

/// <summary>
/// 获取 当前表单值改变的属性集合
Expand Down
30 changes: 0 additions & 30 deletions src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion test/UnitTest/Components/ValidateFormTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down