MudBlazor forms can be validated using Fluent validation in Blazor. I followed the same code found in documentation:
- Created general FluentValueValidator class
public FluentValueValidator(Action<IRuleBuilderInitial<T, T>> rule)
{
rule(RuleFor(x => x));
}
- Created object of that class (ex. dependencyLinkValidator)
public FluentValueValidator<string> dependencyLinkValidator = new FluentValueValidator<string>(x => x
.NotEmpty().WithMessage("{PropertyName} cannot be empty.").WithName("Link"));
- Put value of Validator property to dependencyLinkValidator
Validation="dependencyLinkValidator.Validation"
- 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"));