How can I display multiple global errors in my templates, by their respective error code?
When rejecting on the binding result, the first argument is the error code. How can I use this when displaying the errors in my template?
Use case: I use custom validation rules in my controller (such as duplication checks) and I want to show the global errors on different places in my form.
Ex:
public String myPage(..., BindingResult result) {
result.reject("errorCode1", "Error 1 happened");
result.reject("errorCode2", "Error 2 happened");
return "my-view"
}
In my Thymeleaf template, I can display all errors at once:
<form th:object="${myForm}" method="post">
<p th:if="${#fields.hasGlobalErrors()}" th:errors="*{global}"></p>
</form>
But how can I print only the error with error code errorCode1?