ryanelian / FluentValidation.Blazor

Fluent Validation-powered Blazor component for validating standard <EditForm> :milky_way: :white_check_mark:
https://www.nuget.org/packages/Accelist.FluentValidation.Blazor
MIT License
234 stars 27 forks source link

How to do async validation #21

Closed sven5 closed 4 years ago

sven5 commented 4 years ago

Hi,

Currently, I'm struggling with async validation. I need to call a REST API to check something. My test code is

public class PersonValidator : AbstractValidator<Person>
    {
        public PersonValidator()
        {
            RuleFor(x => x.Name).NotEmpty();
            RuleFor(x => x.Age).InclusiveBetween(18, 80).MustAsync(RunAsync);
        }
        private async Task<bool> RunAsync(int ssss, CancellationToken ct = default)
        {
            await Task.Delay(1000);
            return false;
        }
    }

Do you have some experience how to get it working? Currently this just does nothing.

ryanelian commented 4 years ago

On a glance, that code should run just fine. (That validation logic would be run normally by FluentValidation. I do not tamper with the behavior of any / async validations whatsoever)

Try following these basic troubleshooting guide:

  1. Have you added the validator to the DI?
  2. Is the model used by EditForm component is a Person object too?
  3. Have you put the FluentValidator component inside the EditForm component?
  4. Are you sure that the method is really not running? Try attaching a breakpoint into the custom validation logic.

If all fails, send a zip with minimum repro and I’ll take a quick look at it.

sven5 commented 4 years ago

The method is running, but no validation message appears. Could you take a quick look into this? I've taken the example from https://github.com/mrpmorris/blazor-university/tree/master/src/Forms/CustomValidation

CustomValidation.zip

ryanelian commented 4 years ago

That... does not look like my library at all.

Are you sure that you are using my library? https://www.nuget.org/packages/Accelist.FluentValidation.Blazor

ryanelian commented 4 years ago

By the way, my library has a demo project which you can run here:

https://github.com/ryanelian/FluentValidation.Blazor/tree/master/Demo

Simply clone this repo https://github.com/ryanelian/FluentValidation.Blazor.git or download the zip: https://github.com/ryanelian/FluentValidation.Blazor/archive/master.zip

This demo project has a remote-simulated service:

    public class EmailCheckerService
    {
        public async Task<bool> IsAvailableAsync(string email)
        {
            // simulate delay accessing remote services
            await Task.Delay(1000);

            if (email == "ryan.elian@accelist.com")
            {
                return await Task.FromResult(false);
            }

            return await Task.FromResult(true);
        }
    }

and a validator which uses such service:

    public class FormModel2Validator : AbstractValidator<FormModel2>
    {
        private readonly EmailCheckerService EmailChecker;

        public FormModel2Validator(EmailCheckerService emailCheckerService, IValidator<FormModel3> subValidator)
        {
            this.EmailChecker = emailCheckerService;

            RuleFor(Q => Q.Email).Cascade(CascadeMode.StopOnFirstFailure)
                .NotEmpty().MinimumLength(4).MaximumLength(64).EmailAddress()
                .MustAsync(EmailAvailableAsync).WithMessage(o => $"Email {o.Email} is not available.");

            RuleFor(Q => Q.FormModel3).SetValidator(subValidator);
            RuleForEach(Q => Q.SubArray).SetValidator(subValidator);
        }

        public async Task<bool> EmailAvailableAsync(string email, System.Threading.CancellationToken cancellationToken)
        {
            return await EmailChecker.IsAvailableAsync(email);
        }
    }

which can be run:

image

I urge you to explore this demo project first.

ryanelian commented 4 years ago

That repo / project you gave me, it does not show

<PackageReference Include="Accelist.FluentValidation.Blazor" Version="2.1.0" />

in the csproj file: https://github.com/mrpmorris/blazor-university/blob/master/src/Forms/CustomValidation/CustomValidation.csproj

image


If you are depending on other FluentValidation libraries for Blazor, I suggest that you reach to the author of the said package.

Their package may have bugs / issues with async validation, which I'm not willing to debug.

sven5 commented 4 years ago

Hi @ryanelian,

no need for frustration. I didn't say that it has something to do with your library. I just found your library while searching for async validation in Blazor and I still wonder how you got it working. I will give it a try.

Thanks Sven