SpringSource/spring-framework — pull request #30 by scottfrederick (2012-02-08 04:26:05+00:00)
Title: Validation

This pull request is in response to this Jira issue: https://jira.springsource.org/browse/SPR-4774. It enables the kind of Validator class shown in the example in the last comment on the issue.

This code works great in a modified version of the PetClinic app I am using to test, by setting this AnnotationMethodValidator as the one system-level validator (i.e. by returning an object of this type from the getValidator() method of a config class that extends WebMvcConfigurerAdapter).

If an approach to validation like this one makes sense, then the next question is how to register this validation mechanism with the framework. In my opinion, registering both a LocalValidatorFactoryBean and an AnnotationMethodValidator by default would cover most validation needs in an application, reduce the need to configure custom validators in ConfigurableWebBindingInitializers or @InitBinder methods, and bring validation more in line with other parts of the framework that leverage component scanning and flexible method signatures to make it easier for developers to extend the framework.

Unfortunately, the concept of a single validator class goes pretty deep into the framework (through the Java Config extension points to the DataBinder). It seems like it would be much more flexible for an app to be able to register multiple validators, similar to just about every other extension point (formatters, message converters, argument resolvers, handler exception resolvers, view resolvers, resource handlers, etc...). I have started to make all the changes to support registering a set of validators and defaulting, but I would like to get guidance and advice that this is the right approach before continuing.