Asynchronous fluent validation in Blazor using Mudblazor

Viewed 1534

MudBlazor forms can be validated using Fluent validation in Blazor. I followed the same code found in documentation:

  1. Created general FluentValueValidator class
public FluentValueValidator(Action<IRuleBuilderInitial<T, T>> rule)
{
    rule(RuleFor(x => x));
}
  1. Created object of that class (ex. dependencyLinkValidator)
public FluentValueValidator<string> dependencyLinkValidator = new FluentValueValidator<string>(x => x
        .NotEmpty().WithMessage("{PropertyName} cannot be empty.").WithName("Link"));
  1. Put value of Validator property to dependencyLinkValidator
Validation="dependencyLinkValidator.Validation" 
  1. The form gets validated when user types a new value in textbox but I also call Form.Validate() when user clicks submit button to validate all controls in the form
Form.Validate();

The example is simple and it works perfectly even with custom validators, the problem is, when I create custom validator that uses async function, the validation doesn't work. Is there any way to call Form.Validate() asynchronously?

Example with custom async validator:

 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"));
0 Answers
Related