我有一个 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>
在<ul>
内我收到每个错误 providers[].url
作为e.fieldName
.我认为它会有一些像 providers[0].url
这样的索引等等
所以我的问题是,如何访问字段 providers
在 hasErrors
内正确显示错误消息的方法。
编辑
Controller :
@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";
}
最佳答案
您无法从 Set
中获取项目使用他们的索引,因为集合没有顺序。 Set
接口(interface)不提供基于索引获取项目的方法,所以做 .get(index)
到 Set
会给你编译错误。使用 List
反而。这样,您就可以使用它们的索引访问对象。
所以改变Set<Provider> providers
到:
@Valid
List<Provider> providers;
不要忘记 @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>
编辑
我看到您想集中访问错误,而不是遍历它们。在这种情况下,您可以创建自定义 JSR 303 验证器。请参阅以下有用的代码片段:
用法
@ProviderValid
private List<Provider> providers;
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 {};
}
约束验证器
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;
}
}
完成这些之后,您可以轻松获得您在@ProviderValid
中指定的默认消息。注释如果 ProviderValidator#isValid
只需执行 #fields.hasErrors('providers')
即可返回 false
关于spring-mvc - 用于显示列表错误的表单绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34200637/