Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 400
Sachin Kainth Avatar asked Jun 11 '12 16:06

Sachin Kainth


1 Answers

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)
{
    ...
}
like image 147
Darin Dimitrov Avatar answered Oct 04 '22 02:10

Darin Dimitrov