title: [Spring MVC - Thymeleaf]- Form validation and error messages
body: <p>I´m trying to create a form and validate its data via @Valid on the command object. 
  The validation performs well, but an error is ocurring going back to web. </p>
  
  <p>This is what I have: </p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;div id="content" layout:fragment="contenido"&gt;
      &lt;div sec:authorize="isAnonymous()"&gt;
          &lt;form class="form-horizontal" action="#" th:action="@{register}" th:object="${userForm}" method="post"&gt;
              &lt;input type="hidden" name="_csrf" th:value="${_csrf.token}"/&gt;
              &lt;fieldset&gt;
                  &lt;label for="alias" th:text="#{form.register.alias}"&gt;Alias&lt;/label&gt;
                  &lt;input id="alias" type="text" th:field="*{alias}" placeholder="Su alias" required="required" autofocus="autofocus"/&gt;
  
                  &lt;label for="pass" th:text="#{form.register.password}"&gt;Contraseña&lt;/label&gt;
                  &lt;input id="pass" type="password" th:field="*{password}" pattern="[\w\d-_]{5,15}" required="required" th:title="#{form.error.password}"/&gt;
                  &lt;p th:if="${#fields.hasErrors('password')}" th:errors="*{password}"&gt;Error en el dato ingresado&lt;/p&gt;
  
                  &lt;button type="submit" name="save" class="btn btn-primary" th:text="#{control.register}"&gt;Registrarme&lt;/button&gt;
              &lt;/fieldset&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>@RequestMapping(value = "/register", params = {"save"}, method = RequestMethod.POST) 
  public String register (final ModelMap model, @Valid final UsuarioForm userForm, final BindingResult result) { 
      if (result.hasErrors()) { 
          return "register"; 
      } else { 
          return "redirect:/" + HomeController.PAGE_NAME; 
      } 
  } 
  </code></pre>
  
  <p>When Clicking on "submit" the "register" method is called, result.hasErrors() is true so the same page  should be displayed, but this error occurs. </p>
  
  <p><strong>Stack</strong></p>
  
  <pre><code>java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'userForm' available as request attribute 
          org.springframework.web.servlet.support.BindStatus.&lt;init&gt;(BindStatus.java:144) 
          org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:396) 
          org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:323) 
          org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:289) 
          org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:98) 
          org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87) 
          org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212) 
          org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017) 
          org.thymeleaf.dom.Node.processNode(Node.java:972) 
  </code></pre>
  
  <p>If I add "userForm" to the model in the Controller this way: </p>
  
  <p><strong>Controller Modified</strong></p>
  
  <pre><code>@RequestMapping(value = "/register", params = {"save"}, method = RequestMethod.POST) 
  public String register (final ModelMap model, @Valid final UsuarioForm userForm, final BindingResult result) { 
      if (result.hasErrors()) { 
          model.addAttribute("userForm", userForm); //THIS LINE IS ADDED 
          return "register"; 
      } else { 
          return "redirect:/" + HomeController.PAGE_NAME; 
      } 
  } 
  </code></pre>
  
  <p>The error disappears, BUT... the expression in the HTML <code>${#fields.hasErrors('password')}</code> results false, so I cant show the error messages to the user. </p>
  
  <p>Any idea of why this behaviour is happening?
  Thanks in advance!</p>
  
  <p>PS: I am using Spring MVC 4.1.2 with Thymeleaf 2.1.4</p>