Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable a model validation on an action method in asp.net core

I want to disable a model validation on a specific Action method in a Controller. I have this scenario :

public class SomeDto
{
    public string Name { get; set; }
}

public class SomeDtoValidator : AbstractValidator<SomeDto>, ISomeDtoValidator
{
    public SomeDtoValidator ()
    {
        RuleFor(someDto=> someDto.Name).NotNull().WithMessage("Name property can't be null.");
    }
}

I have ISomeDtoValidator because I register all my validators in my own way :

public class Startup
{
    // .... constructor ....

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOptions();
        services.AddMvc(setup => {
        //...others setups...
        }).AddFluentValidation();

        services.RegisterTypes(Configuration)
                .RegisterFluentValidation()
                .RegisterMappingsWithAutomapper()
                .RegisterMvcConfigurations();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IConfiguration configuration)
    {
        // ...
    }
}

I have this Action Method inside a Controller :

[DisableFormValueModelBinding]
public async Task<IActionResult> CreateSome(Guid someId, SomeDto someDto)
{
    //..... Manually binding someDto...

    var validator = new SomeValidator();
    validator.Validate(someDto);

    // Doing something more ........

    return Ok();
}       

I disabled the modelBinding's MVC because I want to do something before binding SomeDto and, therefore, I don't want to apply any validator on SomeDto either. So, there is any way to achieve that ?? For example, something like this :

[DisableValidator] // Or [DisableValidator(typeof(SomeDtoValidator))] whatever
[DisableFormValueModelBinding]
public async Task<IActionResult> CreateSome(Guid someId, SomeDto someDto)
{
    //..... Manually binding someDto...

    var validator = new SomeValidator();
    validator.Validate(someDto);

    // Doing something more ........

    return Ok();
}   
like image 428
Guille Avatar asked Apr 12 '18 20:04

Guille


People also ask

What is ModelState in asp net core?

Model state represents errors that come from two subsystems: model binding and model validation. Errors that originate from model binding are generally data conversion errors. For example, an "x" is entered in an integer field.

How can disable validation on button click in ASP.NET MVC?

To disable validation when clicking a button, set the MVC Button. CausesValidation property value to false.

What exactly does ModelState IsValid do?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process.


2 Answers

You can skip validation by adding the following attribute to your action methods parameter:

public ActionResult Save([CustomizeValidator(Skip = true)] Customer model) {
  // ...
}

This is described in the docs here: https://docs.fluentvalidation.net/en/latest/aspnet.html#validator-customization

like image 175
Christopher Haws Avatar answered Sep 21 '22 12:09

Christopher Haws


You can use RuleSets.

see: https://fluentvalidation.net/start#rulesets

usage: https://fluentvalidation.net/aspnet#validator-customization

public class CustomerValidator : AbstractValidator<Customer>
{
    public CustomerValidator()
    {
        RuleFor(customer => customer.Surname).NotEmpty();
        RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Please specify a first name");
        RuleFor(customer => customer.Address).Length(20, 250);

        RuleSet("Names", () =>
        {
            RuleFor(x => x.Surname).NotNull();
            RuleFor(x => x.Forename).NotNull();
        });

        RuleSet("Empty", () =>
        {
        });
    }
}

Test code:

        Customer customer = new Customer();
        var validator = new CustomerValidator();
        var validationResult = validator.Validate(customer, ruleSet: "Names");
        // 2 errors: Surname and Forname
        validationResult = validator.Validate(customer, ruleSet: "Empty");
        // no errors

You can configure RuleSet in controller:

public ActionResult SaveNoValidate([CustomizeValidator(RuleSet="Empty")] Customer customer) {
// ...
}

public ActionResult SaveValidateNames([CustomizeValidator(RuleSet="Names")] Customer customer ) {
// ...
}
like image 25
Alexey.Petriashev Avatar answered Sep 20 '22 12:09

Alexey.Petriashev