asp.net-mvc-3entity-framework-4partial-classesfluentvalidation

FluentValidation validator not being called


I have a MVC 3 site but am using the non-MVC FluentValidation dll. I've created a validator class and in the constructor put all my RuleFors and then set an attribute on my model class thus

[FluentValidation.Attributes.Validator(typeof(MyValidator))]

The problem is that the constructor on the validator class never gets called. I think it might be because I am not using the MVC version of the dll, but then I could not get that version to work for me either.

Any help would be appreciated.

Thanks,

Sachin


Solution

  • In your Application_Start make sure that you have initialized the custom fluent validation model validator provider otherwise nothing will happen:

    FluentValidation.Mvc.FluentValidationModelValidatorProvider.Configure();
    

    The FluentValidationModelValidatorProvider class is defined inside the FluentValidation.Mvc assembly. Please take a look at the documentation for integrating FluentValidation into an ASP.NET MVC site.

    The validator will be triggered when you invoke a controller action taking a model decorated with the [Validator] attribute as argument:

    [HttpPost]
    public ActionResult Process(MyViewModel model)
    {
        ...
    }