微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用 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 举报,一经查实,本站将立刻删除。