I have a typical Spring Boot (3.3.2) MVC application using validation and thymeleaf (3.1.2.RELEASE), and I’m finding that Thymeleaf seems to disregard the i18n messages specified in the validation annotation, attempting some other instead. In other words, if an annotation specifies “{A}” as a message, thymeleaf instead tries to use a different message key which seems to be the name of the annotation+the path of the property (e.g., for @Size attached to a “name” property in a “test” bean, it tries to use “Size.test.name” instead).
Distilling the problem, the pieces look as follows
Bean:
public String getName() {
public void setName(String name) {
@NotNull(message = "{ui.errors.required}")
@Size(min = 7, max = 100, message = "{ui.errors.nameLength}")
<code>@Validated
public class TestDTO {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@NotNull(message = "{ui.errors.required}")
@Size(min = 7, max = 100, message = "{ui.errors.nameLength}")
private String name;
}
</code>
@Validated
public class TestDTO {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@NotNull(message = "{ui.errors.required}")
@Size(min = 7, max = 100, message = "{ui.errors.nameLength}")
private String name;
}
Controller:
<code>package org.agoraspeakers.server.controllers.test;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
public class TestController {
@GetMapping("/test-start")
public String startClubRegistration(final HttpServletRequest request, Model model) {
TestDTO test = new TestDTO();
model.addAttribute("test", test);
@PostMapping("/test-post")
public String registerClub(@Valid @ModelAttribute("test") TestDTO test, BindingResult bindingResult, final HttpServletRequest request, Model model) {
System.out.println(bindingResult);
<code>package org.agoraspeakers.server.controllers.test;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class TestController {
@GetMapping("/test-start")
public String startClubRegistration(final HttpServletRequest request, Model model) {
TestDTO test = new TestDTO();
model.addAttribute("test", test);
return "test-form";
}
@PostMapping("/test-post")
public String registerClub(@Valid @ModelAttribute("test") TestDTO test, BindingResult bindingResult, final HttpServletRequest request, Model model) {
System.out.println(bindingResult);
return "test-form";
}
}
</code>
package org.agoraspeakers.server.controllers.test;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class TestController {
@GetMapping("/test-start")
public String startClubRegistration(final HttpServletRequest request, Model model) {
TestDTO test = new TestDTO();
model.addAttribute("test", test);
return "test-form";
}
@PostMapping("/test-post")
public String registerClub(@Valid @ModelAttribute("test") TestDTO test, BindingResult bindingResult, final HttpServletRequest request, Model model) {
System.out.println(bindingResult);
return "test-form";
}
}
Form
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<form th:action="@{/test-post}" th:object="${test}" method="POST" enctype="multipart/form-data">
<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>
<input type="text" th:field="*{name}" size="60">
<button type="submit">Submit</button>
<code><!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body >
<form th:action="@{/test-post}" th:object="${test}" method="POST" enctype="multipart/form-data">
<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>
<input type="text" th:field="*{name}" size="60">
<button type="submit">Submit</button>
</body>
</html>
</code>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body >
<form th:action="@{/test-post}" th:object="${test}" method="POST" enctype="multipart/form-data">
<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>
<input type="text" th:field="*{name}" size="60">
<button type="submit">Submit</button>
</body>
</html>
Now, for some reason thymeleaf insists in rendering some weird “Size.test.name” message key instead of the message that gets properly expanded by the validation framework.
Specifically, the printed bindingResults show:
<code>Field error in object 'test' on field 'name': rejected value []; codes [Size.test.name,Size.name,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [test.name,name]; arguments []; default message [name],50,2]; default message [The name must be between 7 and 100 characters.]
<code>Field error in object 'test' on field 'name': rejected value []; codes [Size.test.name,Size.name,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [test.name,name]; arguments []; default message [name],50,2]; default message [The name must be between 7 and 100 characters.]
</code>
Field error in object 'test' on field 'name': rejected value []; codes [Size.test.name,Size.name,Size.java.lang.String,Size]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [test.name,name]; arguments []; default message [name],50,2]; default message [The name must be between 7 and 100 characters.]
where the custom message “ui.errors.nameLength” from the @Size annotation has been correctly expanded to “The name must be between 7 and 100 characters.”
Why is thymeleaf trying to use a different message, and how can I force it to use the messages that are already specified?
After a lot of debugging, I have a hunch that it might be related to the shouldRenderDefaultMessage
() method in SpringValidatorAdapter
that is returning false, but not quite what to do about it, and I might be totally off the track on this one
<code>public boolean shouldRenderDefaultMessage() {
return this.adapter != null && this.violation != null ? this.adapter.requiresMessageFormat(this.violation) : SpringValidatorAdapter.containsSpringStylePlaceholder(this.getDefaultMessage());
<code>public boolean shouldRenderDefaultMessage() {
return this.adapter != null && this.violation != null ? this.adapter.requiresMessageFormat(this.violation) : SpringValidatorAdapter.containsSpringStylePlaceholder(this.getDefaultMessage());
}
</code>
public boolean shouldRenderDefaultMessage() {
return this.adapter != null && this.violation != null ? this.adapter.requiresMessageFormat(this.violation) : SpringValidatorAdapter.containsSpringStylePlaceholder(this.getDefaultMessage());
}
I searched around here for similar issues but I didn’t manage to find one.
Anyway, my expectation is that thymeleaf should use the messages explicitly specified in the validation. After all, the whole point of explicitly indicating message in a validation annotation is to override any defaults.