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>
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.
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.
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.
I think you may be having the same issue as I did - please see :
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}" }
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With