用于显示列表错误的表单绑定

bar*_*892 3 spring-mvc thymeleaf

我有一个Product包含Set<Provider> providers. 我已经在 Provider 中注释了一个变量url@NotEmpty现在我想显示一个错误,如果这个字段是空的。我不知道我怎么可以访问现场providers的中hasErrors方法正确。

形式:

<form action="#" th:action="@{/saveDetails}" th:object="${selectedProduct}" method="post">

  <!-- bind each input field to list (working) -->
  <input th:each="provider, status : ${selectedProduct.providers}"
         th:field="*{providers[__${status.index}__].url}" />

  <!-- all the time 'false' -->
  <span th:text="'hasErrors-providers=' + ${#fields.hasErrors('providers')}"></span>
  <span th:text="'hasErrors-providers[0].url=' + ${#fields.hasErrors('providers[0].url')}"></span>

  <!-- not working -->
  <span class="help-block" th:each="provider, status : ${selectedProduct.providers}" 
     th:if="${#fields.hasErrors('providers[__${status.index}__].url')}" 
     th:errors="${providers[__${status.index}__].url}">Error Url
  </span>

  <!-- print errors (just for testing purpose) -->
    <ul>
      <li th:each="e : ${#fields.detailedErrors()}">
        <span th:text="${e.fieldName}">The field name</span>|
        <span th:text="${e.code}">The error message</span>
      </li>
    </ul>

</form>
Run Code Online (Sandbox Code Playgroud)

<ul>我收到的每个错误中providers[].url作为e.fieldName. 我认为这将是具有像一些指标providers[0].url等,所以我的问题是,我怎么能访问该字段providers的内hasErrors法正确显示错误消息。

编辑

控制器:

@RequestMapping(value = "/saveDetails", method = RequestMethod.POST)
public String saveDetails(@Valid @ModelAttribute("selectedProduct") final Product selectedProduct,
                          final BindingResult bindingResult, SessionStatus status) {
    if (bindingResult.hasErrors()) {
        return "templates/details";
    }
    status.setComplete();
    return "/templates/overview";
}
Run Code Online (Sandbox Code Playgroud)

Bnr*_*rdo 5

您不能Set使用它们的索引从 a 中获取项目,因为集合没有排序。Set接口不提供基于索引获取项目的方法,因此.get(index)对 aSet进行操作会给您带来编译错误。使用List来代替。这样,您就可以使用对象的索引访问对象。

所以Set<Provider> providers改为:

@Valid
List<Provider> providers;
Run Code Online (Sandbox Code Playgroud)

不要忘记@Valid注释,以便它会向下级联到子对象。

此外,如果th:errors在表单内,它应该指向支持该表单的对象的属性,使用选择表达式 ( *{...})

<span class="help-block" th:each="provider, status : ${selectedProduct.providers}" 
    th:if="${#fields.hasErrors('providers[__${status.index}__].url')}" 
    th:errors="*{providers[__${status.index}__].url}">Error Url
</span>
Run Code Online (Sandbox Code Playgroud)

编辑

我看到您希望共同访问错误,而不是遍历它们。在这种情况下,您可以创建自定义 JSR 303 验证器。请参阅以下有用的代码片段:

用法

@ProviderValid
private List<Provider> providers;
Run Code Online (Sandbox Code Playgroud)

ProviderValid 注释

//the ProviderValid annotation.
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ProviderValidator.class)
@Documented
public @interface ProviderValid {
    String message() default "One of the providers has invalid URL.";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}
Run Code Online (Sandbox Code Playgroud)

约束验证器

public class ProviderValidator implements ConstraintValidator<ProviderValid, List<Provider>>{

    @Override
    public void initialize(ProviderValid annotation) { }

    @Override
    public boolean isValid(List<Provider> value, ConstraintValidatorContext context) {

        //...
        //validate your list of providers here
        //obviously, you should return true if it is valid, otherwise false.
        //...

        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

完成这些操作后,@ProviderValid如果ProviderValidator#isValid返回 false ,您可以轻松获得您在注释中指定的默认消息,只需执行#fields.hasErrors('providers')