title: Spring MVC Thymeleaf error template overriding inline form error messages
body: <p>I'm working on validating a form and showing inline errors for fields that don't pass validation using Thymeleaf. I'm using Spring Boot 1.5.8.</p>
  
  <p>I have a <code>User</code> object that will be bound to the form. It has fields like: <code>username</code> and <code>email</code>. Here's the form declaration,</p>
  
  <pre><code>&lt;form role="form" class="form" method="post"
      th:action="@{'/admin/user/new'}" th:object="${user}"&gt;
  </code></pre>
  
  <p>I set up an error controller like this because I wanted a catch all error page. I also created a template at <code>src/main/resources/templates/error.html</code>. This was because the security context was lost on the error page according to this github <a href="https://github.com/spring-projects/spring-boot/issues/1048" rel="nofollow noreferrer">issue</a>, although it indicates the issue should be fixed in my version. Without this controller the default error template is shown, but I have no access to the security context. With this controller in place and the <code>error.html</code> template any random error will be handled.</p>
  
  <pre><code>@Controller
  public class ErrorController extends BasicErrorController {
  
      /**
       * This Bean declaration provides the Spring Security Context to 4xx responses. This is reported as
       * fixed in Spring Boot 2.0.0:
       *
       * https://github.com/spring-projects/spring-boot/issues/1048
       *
       * @param springSecurityFilterChain
       * @return
       */
      @Bean
      public FilterRegistrationBean getSpringSecurityFilterChainBindedToError(
                      @Qualifier("springSecurityFilterChain") Filter springSecurityFilterChain) {
  
              FilterRegistrationBean registration = new FilterRegistrationBean();
              registration.setFilter(springSecurityFilterChain);
              registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
              return registration;
      }
  
      @Autowired
      public ErrorController(ErrorAttributes errorAttributes) {
          super(errorAttributes, new ErrorProperties());
      }
  
      @RequestMapping(value = "/error")
      public String error(Model model) {
          return "/error";
      }
  
      @Override
      public String getErrorPath() {
          return "/__dummyErrorPath";
      }
  
  }
  </code></pre>
  
  <p>For this example I'm just trying to validate the <code>email</code> field and to show a message if validation fails next to this field. The class field looks like this on <code>User</code>,</p>
  
  <pre><code>@NotNull
  @Email
  private String email;
  </code></pre>
  
  <p>The Thymeleaf field looks like this,</p>
  
  <pre><code>&lt;div th:fragment="email" class="form-group"&gt;
      &lt;label&gt;Email*&lt;/label&gt;
      &lt;input class="form-control" type="text" th:field="*{email}" th:required="required" /&gt;
      &lt;span class="error" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"&gt;...&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The new user form controller method looks like this,</p>
  
  <pre><code>@GetMapping("admin/user/new")
  public String newUser(Model model, HttpServletRequest request) {
      model.addAttribute("user", new User());
      return "admin/user/new";
  }
  </code></pre>
  
  <p>And the corresponding POST method looks like this,</p>
  
  <pre><code>@PostMapping("admin/user/new")
  public String newUser(@Valid @ModelAttribute User user, final ModelMap model, BindingResult bindingResult) {
      Assert.notNull(user, "User must not be null");
      if (bindingResult.hasErrors()) {
          return "admin/user/new";
      }
      userService.save(user);
      model.clear();
      return "redirect:/admin/user/list";
  }
  </code></pre>
  
  <p>In this POST method I'm checking to see if the <code>BindingResult</code> has errors. If it does I'm expecting the <code>admin/user/new</code> template to be rendered with the inline error message shown. However my catch all error template is being rendered. The URL still shows <code>/admin/user/new</code> and refreshing the page still shows the error template.</p>
  
  <p>My main question is how can I use the catch all error page and still show inline errors when I need to if possible?</p>