Thymeleaf: validation error by field

烂漫一生 提交于 2020-06-12 07:57:20

问题


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?


回答1:


Use this code to display error:

<p class="alert alert-danger" th:if="${#fields.hasErrors('companyName')}" th:errors="*{companyName}"></p>


来源:https://stackoverflow.com/questions/41510520/thymeleaf-validation-error-by-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!