使用 Mudblazor 在 Blazor 中进行异步流畅验证

问题描述

MudBlazor 表单可以在 Blazor 中使用 Fluent 验证进行验证。我遵循了文档中的相同代码

  1. 创建通用FluentValueValidator
public FluentValueValidator(Action<IRuleBuilderInitial<T,T>> rule)
{
    rule(RuleFor(x => x));
}
  1. 创建该类的对象(例如dependencyLinkValidator
public FluentValueValidator<string> dependencyLinkValidator = new FluentValueValidator<string>(x => x
        .NotEmpty().WithMessage("{PropertyName} cannot be empty.").WithName("Link"));
  1. 将 Validator 属性的值放入 dependencyLinkValidator
Validation="dependencyLinkValidator.Validation" 
  1. 用户在文本框中键入新值时表单会得到验证,但当用户单击提交按钮以验证表单中的所有控件时,我也会调用 Form.Validate()
Form.Validate();

该示例很简单,即使使用自定义验证器也能完美运行,但问题是,当我创建使用异步函数自定义验证器时,验证不起作用。有没有办法异步调用Form.Validate()?

自定义异步验证器示例:

 public FluentValueValidator<string> dependencyLinkValidator = new FluentValueValidator<string>(x => x
            .NotEmpty().WithMessage("{PropertyName} cannot be empty.")
            .MustAsync(async (x,cancellation) =>
            {
                return await AsyncFunction(x);
            }).WithMessage("Site doesn't exist")
            .WithName("Link"));

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)