Thymeleaf:按字段的验证错误 [英] Thymeleaf: validation error by field

查看:84
本文介绍了Thymeleaf:按字段的验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Thymeleaf + Spring和表单验证,我想在该字段旁边显示与该字段相关的错误.

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?

推荐答案

使用以下代码显示错误:

Use this code to display error:

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

这篇关于Thymeleaf:按字段的验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆