Kacper Fleszar
Kacper Fleszar

Reputation: 166

JPA validation messages internationalization with Thymeleaf

I've met with some issue I cannot solve. I would like to internatiolize JPA validation messages with Spring Thymeleaf.

I've tried a few solutions, but none of them works.

This is my entity class property:

@Length(min = 1, message = "#{field.empty}")
@Valid
private String lastName;

This is thymeleaf form:

<div class="form-group col">
    <label for="lastName" th:text="#{register.lastname}"></label>
    <input id="lastName" class="form-control" type="text"
        th:placeholder="#{register.lastname}"
        th:field="*{lastName}"
        th:classappend="${#fields.hasErrors('lastName')} ? is-invalid : ''"/>
    <small class="form-text text-danger"
        th:if="${#fields.hasErrors('lastName')}"
        th:errors="*{lastName}"></small>
</div>

I expect to print translated error message in the same way as you can see here

<label for="lastName" th:text="#{register.lastname}"></label>

But instead of seeing translated output, I can see #{field.empty}

Is there any way to do so?

Upvotes: 0

Views: 377

Answers (1)

Kacper Fleszar
Kacper Fleszar

Reputation: 166

So thanks to goalkicker Spring book (Chapter 8: Spring JSR 303 Bean validation) I'd dealt with my problem. You can set error messages in messages.property generic for annotation type and for specific class properties:

And for

@Length

use

Length=error message here. 

For this specific field (lastName)

Length.lastName=error message here

The same can be used for all JPA annotations

Upvotes: 3

Related Questions