简体   繁体   English

无法在表单标签之外显示验证消息

[英]Cannot display the validation message outside the form tag

I'm having trouble displaying the validation message outside the form tag.我无法在表单标签之外显示验证消息。

What I've tried我试过的

  • Make sure the return value of "${#fields.errors('${office}')}" is always false.确保 "${#fields.errors('${office}')}" 的返回值始终为 false。
  • Make sure field errors happens when a input value is invalid.确保输入值无效时发生字段错误。

Source Codes源代码

A Part of Controller Controller的一部分

 @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;
    }
    ...
  }

A Part Of Thymeleaf Template Thymeleaf模板的一部分

<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>

I think you have wrong syntax in your template.我认为您的模板中有错误的语法。 Try to display this for all errors:尝试显示所有错误:

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

I don't know your Office model but you can try this for individual error:我不知道您的 Office model,但您可以针对个别错误尝试此操作:

<p th:if="${#fields.hasErrors('yourModelInstance.propertyName')}" 
 class="label label-danger" th:errors="*{yourModelInstance.propertyName}">Error here</p>

I couldn't solve this problem and I resolved this.我无法解决这个问题,我解决了这个问题。 I placed the error messages inside the form tag.我将错误消息放在表单标签内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM