for my project i use two libraries:
- FluentValidation (http://fluentvalidation.codeplex.com/) for view model validation
- MVC Extensions (http://mvcextensions.codeplex.com/) to fluently configure metadata for my view models
Here is how i configure them (this is done in container builder class):
/* Model Metadata Registration */
IEnumerable<IModelMetadataConfiguration> configurations = container.Resolve<IEnumerable<IModelMetadataConfiguration>>();
IModelMetadataRegistry registry = new ModelMetadataRegistry();
configurations.Each(configuration => registry.RegisterModelProperties(configuration.ModelType, configuration.Configurations));
ModelMetadataProviders.Current = new ExtendedModelMetadataProvider(registry);
/* Fluent Validation Configuration */
FluentValidationModelValidatorProvider.Configure(x =>
{
x.ValidatorFactory = container.Resolve<IValidatorFactory>();
x.AddImplicitRequiredValidator = false;
})
Now when i run the application, model will not be validated by FluentValidationModelValidatorProvider. Instead the default mechanism is used. Therefore i had to comment out FluentValidationModelValidatorProvider configuration and use this approach instead:
IValidator validator = _validatorFactory.GetValidator(typeof(RegisterUserCommand));
ValidationResult result = validator.Validate(command);
and then:
if (!result.IsValid)
{
command.Password = String.Empty;
command.ConfirmPassword = String.Empty;
ModelState.Clear();
ModelState.AddModelErrors(result.Errors);
return View(command);
}
But there must be a way to make them work together. Any advice?