Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf not displaying Spring form error messages

I'm migrating a Spring jsp application to Thymeleaf but having problems displaying form errors.

I'm using the SpringTemplateEngine and ThymeleafViewResolver and rendering of templates works. Also form values are populated in form input fields.

The only thing so far not working is displaying form error messages.

My controller looks like:

@RequestMapping(method = RequestMethod.POST) String save(@Valid CustomerForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) {     if (bindingResult.hasErrors()) {         model.addAttribute("form", form)         return "app/customers/create"     }     .... 

I printed the bindingResult to verify it contains an error:

binding result = org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'customerForm' on field 'name': rejected value []; codes [customerForm.name.NotBlank,name.NotBlank,java.lang.String.NotBlank,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [customerForm.name,name]; arguments []; default message [name]]; default message [may not be empty] 

When I try to display error using:

<ul>     <li th:each="e : ${#fields.detailedErrors()}" th:class="${e.global}? globalerr : fielderr">         <span th:text="${e.global}? '*' : ${e.fieldName}">The field name</span> |         <span th:text="${e.message}">The error message</span>     </li> </ul> 

it does not display any error.

I tried various alternatives as documented on http://www.thymeleaf.org/doc/html/Thymeleaf-Spring3.html#validation-and-error-messages but without success.

Am I missing something?

EDIT

Note I'm trying to display the error within a form set via th:object:

<form id="customer-form" action="#" th:action="@{/app/customers}" th:object="${form}" method="post" class="form-horizontal">     <ul>         <li th:each="e : ${#fields.detailedErrors()}" th:class="${e.global}? globalerr : fielderr">             <span th:text="${e.global}? '*' : ${e.fieldName}">The field name</span> |             <span th:text="${e.message}">The error message</span>         </li>     </ul> </form> 
like image 944
Marcel Overdijk Avatar asked Apr 08 '14 21:04

Marcel Overdijk


People also ask

How do I add validation to Thymeleaf?

Click Dependencies and select Spring Web, Thymeleaf, and Validation. Click Generate. Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.

How do I enable Thymeleaf in Spring Boot?

1.1. Spring Boot will provide auto-configuration for Thymeleaf. Add spring-boot-starter-thymeleaf dependency in pom. xml to enable this auto-configuration. No other configurations required, Spring Boot will inject all required configuration to work with Thymeleaf.

Is Thymeleaf fully integrated with Spring?

It provides full integration with Spring Framework. It applies a set of transformations to template files in order to display data or text produced by the application. It is appropriate for serving XHTML/HTML5 in web applications. The goal of Thymeleaf is to provide a stylish and well-formed way of creating templates.


2 Answers

I think you may be having the same issue as I did - please see :

  • Fields object functions (Spring)

There it is answered by Daniel Fernandez. Basically your form object th:object="${form}" is named "form" but your controller is looking for "customerForm" (class name) not "form" (the variable name)

can be renamed with @ModelAttribute("data")

copied from that link use:

public String post(@Valid FormData formData, BindingResult result, Model model){     // th:object="${formData}" } 

or

public String post(@Valid @ModelAttribute("data") FormData data, BindingResult result, Model model){     // th:object="${data}" }  
like image 154
Gary Hellman Avatar answered Sep 30 '22 15:09

Gary Hellman


This is how I do it in my forms:

For displaying all errors I put this at the beginning of my form:

<div class="alert alert-danger" th:if="${#fields.hasErrors('*')}">     <p th:each="err : ${#fields.errors('*')}" th:text="${err}"></p>     </div> 

and for individual error I add this after the field (of course, changing field in hasErrors to correspond to the field tested):

<p th:if="${#fields.hasErrors('vehicle.licensePlate')}" class="label label-danger" th:errors="*{vehicle.licensePlate}">Incorrect LP</p> 

Let me know if this works for you?

like image 23
Blejzer Avatar answered Sep 30 '22 16:09

Blejzer