ros2008
ros2008

Reputation: 21

How can i set cascade mode Globally?

I am new to .net core web API. I am using fluent validation API for model validation. I want to set cascade option globally for all validators. I found following line

ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;  

in Given Link

But I am confused where to write this line and in which function.

Can anyone help?

Upvotes: 2

Views: 1514

Answers (2)

Ricky Gummadi
Ricky Gummadi

Reputation: 5240

The accepted answer is not quite clear, if you are using .NET Core you can set the cascade mode at a global level like below

.AddFluentValidation(fv =>
            {
                fv.RunDefaultMvcValidationAfterFluentValidationExecutes = true;
                fv.ValidatorOptions.CascadeMode = CascadeMode.Stop;

                fv.RegisterValidatorsFromAssemblyContaining<Startup>();
            });

Upvotes: 2

Robert Perry
Robert Perry

Reputation: 1954

That class is a static, so you set it as early in the pipeline as you reasonable can:

To set the cascade mode globally, you can set the CascadeMode property on the static ValidatorOptions class during your application’s startup routine

In a Net Core application that would be in the Startup class

You should have something like this:

public class Startup
{
    // Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        ...
    }

    // Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
        **ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;**  
    }
}

Add it somewhere like that

Upvotes: 2

Related Questions