Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf: validation error by field

Using Thymeleaf + Spring and form validation, I want to display errors that are related to a field next to this field.

<input type="text"
  th:field="*{companyName}" class="form-control"
  placeholder="Code client" th:errorClass="'error'"/>

If the field has an error, the class "error" is applied indeed. But is there an easy way to display also the validation error of the field?

like image 237
Rolintocour Avatar asked Mar 11 '23 05:03

Rolintocour


1 Answers

Use this code to display error:

<p class="alert alert-danger" th:if="${#fields.hasErrors('companyName')}" th:errors="*{companyName}"></p>
like image 91
Lukas Grygar Avatar answered Mar 17 '23 07:03

Lukas Grygar