如何解决使用 Mudblazor 在 Blazor 中进行异步流畅验证
MudBlazor 表单可以在 Blazor 中使用 Fluent 验证进行验证。我遵循了文档中的相同代码:
- 创建通用FluentValueValidator类
public FluentValueValidator(Action<IRuleBuilderInitial<T,T>> rule)
{
rule(RuleFor(x => x));
}
- 创建该类的对象(例如dependencyLinkValidator)
public FluentValueValidator<string> dependencyLinkValidator = new FluentValueValidator<string>(x => x
.NotEmpty().WithMessage("{PropertyName} cannot be empty.").WithName("Link"));
- 将 Validator 属性的值放入 dependencyLinkValidator
Validation="dependencyLinkValidator.Validation"
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 举报,一经查实,本站将立刻删除。