我无法在form标签外显示验证消息。
@PostMapping("/for_office/office_modify")
public String updateOffice(
Locale locale,
Model model,
RedirectAttributes attrs,
@Valid @ModelAttribute Office office,
BindingResult bindingResult)
throws ParseException {
attrs.addFlashAttribute("templateName", ViewNameConst.OFFICE_VIEW_NAME);
if (bindingResult.hasErrors()) {
logger.info("OfficeController:updateOffice:hasErrors");
logger.info(bindingResult.toString());
return ViewNameConst.OFFICE_VIEW_NAME;
}
...
}
<ul class="errMsg text-danger" th:if="${#fields.hasErrors('${office}')}">
<li th:each="err : ${#fields.errors('${office}')}" th:text="${err}">
Input is incorrect
</li>
</ul>
答案 0 :(得分:0)
我认为您的模板语法错误。 尝试针对所有错误显示此信息:
<div class="text-danger" th:if="${#fields.hasErrors('*')}">
<p th:each="err : ${#fields.errors('*')}" th:text="${err}"></p>
</div>
我不知道您的Office模型,但是您可以针对个别错误尝试以下方法:
<p th:if="${#fields.hasErrors('yourModelInstance.propertyName')}"
class="label label-danger" th:errors="*{yourModelInstance.propertyName}">Error here</p>
答案 1 :(得分:0)
我无法解决此问题,因此我解决了。我将错误消息放在了表单标签中。