title: Spring 4.0 form validation issue using AJAX
body: <p>I am facing problem when i am doing server side form validation using jQuery and spring 4. Every time "result" (object of BindingResult) is coming with zero error result.</p>
  
  <p>Below I am proving my code for better understanding.
  my jsp code is</p>
  
  <pre><code>  &lt;div class="registerForm"&gt;
                        &lt;div class="modal-header"&gt;
                          &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
                          &lt;h4 class="modal-title" id="myModalLabel"&gt;Please Register&lt;/h4&gt;
                        &lt;/div&gt;
                        &lt;div class="modal-body"&gt;
                              &lt;form class="form-signin" id="regForm" commandName="userRegister"&gt;
                                  &lt;div class="reg-error error"&gt;&lt;/div&gt;&lt;div class="reg-success success"&gt;&lt;/div&gt;
                                  &lt;input type="text" name="username" class="input-block-level" placeholder="Username"&gt;
                                  &lt;input type="text" name="emailAddress" class="input-block-level" placeholder="Email address"&gt;
                                  &lt;input type="text" name="confirmEmailAdd" class="input-block-level" placeholder="Confirm Email address"&gt;
                                  &lt;input type="password" name="password" class="input-block-level" placeholder="Password"&gt;
                              &lt;/form&gt;
                        &lt;/div&gt;
                        &lt;div class="modal-footer"&gt;
                          &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt;
                          &lt;button type="button" class="btn btn-primary submitRegister"&gt;Register&lt;/button&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
  </code></pre>
  
  <p>my script code is</p>
  
  <pre><code> $.ajax({
                  url: "${pageContext.request.contextPath}/register",    
                  data: $('#regForm').serialize(), 
                  type: "POST",
                  success: function(result) {},
                  error: function(XMLHttpRequest, textStatus, errorThrown){}
              });
  </code></pre>
  
  <p>My controller code is </p>
  
  <pre><code>@RequestMapping(value="/register", method = RequestMethod.POST)
      public @ResponseBody 
      ValidationResponse register(Model model, @ModelAttribute("userRegister") @Valid UserRegister userReg, BindingResult result) {
          ValidationResponse res = new ValidationResponse();
  
          if(result.hasErrors()){
              res.setStatus("FAIL");
              List&lt;FieldError&gt; allErrors = result.getFieldErrors();
              List&lt;ErrorMessage&gt; errorMesages = new ArrayList&lt;ErrorMessage&gt;();
              for (FieldError objectError : allErrors) {
                  errorMesages.add(new ErrorMessage(objectError.getField(), objectError.getField() + "  " + objectError.getDefaultMessage()));
              }
              res.setErrorMessageList(errorMesages);
  
          } else {
              res.setStatus("SUCCESS");
          }
          return res;
      }
  </code></pre>
  
  <p>My bean object</p>
  
  <pre><code>public class UserRegister {
  
      @NotEmpty @NotNull
      private String username;
  
      @NotEmpty @NotNull
      private String password;
  
      @NotEmpty @Email @NotNull
      private String emailAddress;
  
  setter and getters....
  }
  </code></pre>
  
  <p>Do i need to do any other configuration or something else to get it validated properly?</p>
  
  <p>app-servlet.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="
  http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                          http://www.springframework.org/schema/context
                          http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"&gt;
  
  
      &lt;context:property-placeholder location="classpath:database.properties" /&gt;
      &lt;context:component-scan base-package="com.app" /&gt;
  
      &lt;tx:annotation-driven transaction-manager="hibernateTransactionManager"/&gt;
  
      &lt;bean id="myAuthenticationSuccessHandler" class="com.app.security.handler.MySimpleUrlAuthenticationSuccessHandler" /&gt;
      &lt;bean id="myAuthenticationFailureHandler" class="com.app.security.handler.MySimpleUrlAuthenticationFailureHandler" /&gt;
  
      &lt;bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="viewClass"
              value="org.springframework.web.servlet.view.JstlView" /&gt;
          &lt;property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="${database.driver}" /&gt;
          &lt;property name="url" value="${database.url}" /&gt;
          &lt;property name="username" value="${database.user}" /&gt;
          &lt;property name="password" value="${database.password}" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;             
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- bind your messages.properties
      &lt;bean class="org.springframework.context.support.ResourceBundleMessageSource"
          id="messageSource"&gt;
          &lt;property name="basename" value="messages" /&gt;
      &lt;/bean&gt;
           --&gt;
  &lt;/beans&gt;
  </code></pre>