title: Displaying "application-specific" validation errors in web UI using Thymeleaf's "fields.hasErrors"
body: <p>I use Spring MVC together with Thymeleaf. I have an issue with Thymeleaf <code>fields.hasErrors</code> which is as follows:</p>
  
  <p>When a <strong>Bean Validation constraint</strong> error is raised (such as a @NotNull constraint), the following use of <code>fields.hasErrors</code> works perfectly:</p>
  
  <pre><code>&lt;span class="help-inline" th:if="${#fields.hasErrors('member.email')}" th:text="#{message_form.validation.email}"&gt;&lt;/span&gt;
  </code></pre>
  
  <p>An error message is displayed OK next to the email field.</p>
  
  <p>However, when an <strong>"application-specific" error</strong> (as opposed to <strong>Bean Validation constraint error</strong>) is raised by my code (see code from controller below),</p>
  
  <pre><code>// FROM SPRING MVC CONTROLLER
  if (!registrationService.isEmailAvailable(registrationInfo.getMember().getEmail())) {
      bindingResult.addError(new ObjectError("member.email", "email already used")); // TODO i18n
  }
  </code></pre>
  
  <p>The <code>th:if="${#fields.hasErrors('member.email')}"</code> <strong>does not evaluate to true</strong> and no error message is displayed....</p>
  
  <p>The only way I have found to display those "application-specific" errors is to include the following underneath the form:</p>
  
  <pre><code>&lt;div id="errors" class="alert alert-error"&gt;
      &lt;ul th:if="${#fields.hasErrors('*')}"&gt;
          &lt;li th:each="err : ${#fields.errors('*')}" th:text="${err}"&gt;&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Then, it will display "<em>email already used</em>" as a list item.</p>
  
  <p>Can anyone please clarify the behavior of <code>th:if="${#fields.hasErrors('member.email')}"</code> and especially tell me why it will <strong>evaluate to false</strong> in the case of <strong>"application specific" errors</strong>?</p>