我在表单标签外显示验证消息时遇到问题。
我尝试过什么
- 确保返回值"${#fields.errors('${office}'(}"始终为 false。
- 确保在输入值无效时发生字段错误。
源代码
控制器的一部分
@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>
我认为您的模板中有错误的语法。 尝试为所有错误显示此信息:
<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>
我无法解决这个问题,我解决了这个问题。我将错误消息放在表单标签内。