title: Validation with Spring 3.2.0
body: <p>I'm using <a href="http://sourceforge.net/projects/hibernate/files/hibernate-validator/4.3.1.Final/" rel="nofollow">HibernateValidator 4.3.1</a>. Validations are performed as intended throughout the entire application.</p>
  
  <p>I have registered some custom editors to perform validation globally such as for ensuring numeric values (<code>double</code>, <code>int</code> etc) in a text-field, for ensuring valid dates regarding the <a href="http://www.joda.org/joda-time/" rel="nofollow">Joda-Time</a> API etc.</p>
  
  <p>In this type of validation, I'm allowing null/empty values by setting the <code>allowEmpty</code> parameter to <code>false</code> as usual to validate it separately especially for displaying separate user friendly error messages when such fields are left blank.</p>
  
  <p>Therefore, in addition to validating with HibernateValidator and custom editors, I'm trying to use the following validation strategy. Again, this kind of validation is only for those fields which are registered for custom editors <strong>are when left blank</strong>.</p>
  
  <p>The following is the class that implements the <a href="http://static.springsource.org/autorepo/docs/spring/3.2.0.M1/api/org/springframework/validation/Validator.html" rel="nofollow"><code>org.springframework.validation.Validator</code></a> interface.</p>
  
  <pre><code>package test;
  
  import org.springframework.stereotype.Component;
  import org.springframework.validation.Errors;
  import org.springframework.validation.ValidationUtils;
  import org.springframework.validation.Validator;
  import validatorbeans.TempBean;
  
  @Component
  public final class TempValidator implements Validator {
  
      @Override
      public boolean supports(Class&lt;?&gt; clazz) {
          System.out.println("supports() invoked.");
          return TempBean.class.isAssignableFrom(clazz);
      }
  
      @Override
      public void validate(Object target, Errors errors) {
          TempBean tempBean = (TempBean) target;
  
          System.out.println("startDate = " + tempBean.getStartDate() + " validate() invoked.");
          System.out.println("doubleValue = " + tempBean.getDoubleValue() + " validate() invoked.");
          System.out.println("stringValue = " + tempBean.getStringValue() + " validate() invoked.");
  
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "startDate", "java.util.date.nullOrEmpty.error");
          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "doubleValue", "java.lang.double.nullOrEmpty.error");
      }
  }
  </code></pre>
  
  <p>The class is designated with the <code>@Component</code> annotation so that it can be auto-wired to a specific Spring controller class. The debugging statements display exactly based on the input provided by a user.</p>
  
  <p>The following is the controller class.</p>
  
  <pre><code>package controller;
  
  import customizeValidation.CustomizeValidation;
  import java.util.Map;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.validation.Valid;
  import javax.validation.groups.Default;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.validation.BindingResult;
  import org.springframework.validation.DataBinder;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import test.TempValidator;
  import validatorbeans.TempBean;
  
  @Controller
  public final class TempController {
  
      @Autowired
      private TempService tempService;
  
      private TempValidator tempValidator;
  
      public TempValidator getTempValidator() {
          return tempValidator;
      }
  
      @Autowired
      public void setTempValidator(TempValidator tempValidator) {
          this.tempValidator = tempValidator;
      }
  
      @RequestMapping(method = {RequestMethod.GET}, value = {"admin_side/Temp"})
      public String showForm(@ModelAttribute("tempBean") @Valid TempBean tempBean, BindingResult error, Map model, HttpServletRequest request, HttpServletResponse response) {
          return "admin_side/Temp";
      }
  
      @RequestMapping(method = {RequestMethod.POST}, value = {"admin_side/Temp"})
      public String onSubmit(@ModelAttribute("tempBean") @Valid TempBean tempBean, BindingResult errors, Map model, HttpServletRequest request, HttpServletResponse response) {
          //tempValidator.supports(TempBean.class);
          //tempValidator.validate(tempBean, errors);
  
          DataBinder dataBinder = new DataBinder(tempBean);
          dataBinder.setValidator(tempValidator);
          dataBinder.validate();
  
          //errors=dataBinder.getBindingResult();
          if (CustomizeValidation.isValid(errors, tempBean, TempBean.ValidationGroup.class, Default.class) &amp;&amp; !errors.hasErrors()) {
              System.out.println("Validated");
          }
  
          return "admin_side/Temp";
      }
  }
  </code></pre>
  
  <p>I'm invoking the validator from the Spring controller class itself (which I indeed want) by </p>
  
  <pre><code>DataBinder dataBinder = new DataBinder(tempBean);
  dataBinder.setValidator(tempValidator);
  dataBinder.validate();
  </code></pre>
  
  <p>The validator is called but the validation which is expected is <strong>not</strong> performed.</p>
  
  <p>If only I invoke the validator manually using the following statement (which is commented out above),</p>
  
  <pre><code>tempValidator.validate(tempBean, errors);
  </code></pre>
  
  <p>then validation is performed. So I don't believe my validator is correctly working. Why does it fail to work with <code>DataBinder</code>?</p>
  
  <p>In my <code>application-context.xml</code> file, this bean is simply configured as follows.</p>
  
  <pre><code>&lt;bean id="tempValidator" class="test.TempValidator"/&gt;
  </code></pre>
  
  <p>This many packages as below including the <code>test</code> package which the <code>TempValidator</code> class is enclosed within are auto-detected.</p>
  
  <pre><code>&lt;context:component-scan base-package="controller spring.databinder validatorbeans validatorcommands test" use-default-filters="false"&gt;
      &lt;context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/&gt;
      &lt;context:include-filter expression="org.springframework.web.bind.annotation.ControllerAdvice" type="annotation"/&gt;  
  &lt;/context:component-scan&gt;
  </code></pre>
  
  <p>I have even tried to put </p>
  
  <pre><code>&lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/&gt;
  </code></pre>
  
  <p>In my <code>dispatcher-servlet.xml</code> file.</p>
  
  <p>What am I overlooking here?</p>