title: Spring MVC and JSR 303 - Manual Validation
body: <p>I'm using Spring MVC 3 and JSR 303. I have a form backing object that has beans of different types in it.  Depending on a request parameter value, I'll have to choose a bean to validate and save. I can't use <code>@Valid</code> for validation since the bean to validate is not known until the runtime.</p>
  
  <p>I was able to inject a <code>javax.validation.Validator</code> to the controller, but I'm not sure how to validate a bean with it and store any errors in a <code>BindingResult/Error</code> in a "Spring way".</p>
  
  <p>I need to do it in the handler method, rather than an <code>initBinder</code> method, because of request mapping.</p>
  
  <p>[edit]</p>
  
  <p>The problem I'm having with <code>validate(Object, Errors)</code> is that it doesn't recognize nested beans. The bean to validate is accessed through foo.getBar().getBean(), where the foo is the form backing object. When I do <code>validate(foo.getBar().getBean(), errors)</code>, I get the following error message.</p>
  
  <pre><code>JSR-303 validated property 'property-name' does not have a corresponding accessor for Spring data binding
  </code></pre>
  
  <p>Has anyone done something like this before? Thanks.</p>