用于显示列表错误的表单绑定
Form binding for showing errors of a list
我有一个包含 Set<Provider> providers
的 Product
对象。我在 Provider 中用 @NotEmpty
注释了一个变量 url
现在我想显示一个错误,如果这个字段是空的。
我不确定如何正确访问 hasErrors
方法中的字段 providers
。
表格:
<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
等。
所以我的问题是,如何正确访问 hasErrors
方法中的字段 providers
以显示错误消息。
编辑
控制器:
@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
中获取项目,因为集合没有顺序。 Set
接口不提供根据索引获取项目的方法,因此对 Set
执行 .get(index)
会导致编译错误。请改用 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;
}
}
完成这些后,如果 ProviderValidator#isValid
returns false 只需执行 #fields.hasErrors('providers')
[=27= 即可轻松获得在 @ProviderValid
注释中指定的默认消息]
我有一个包含 Set<Provider> providers
的 Product
对象。我在 Provider 中用 @NotEmpty
注释了一个变量 url
现在我想显示一个错误,如果这个字段是空的。
我不确定如何正确访问 hasErrors
方法中的字段 providers
。
表格:
<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
等。
所以我的问题是,如何正确访问 hasErrors
方法中的字段 providers
以显示错误消息。
编辑
控制器:
@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
中获取项目,因为集合没有顺序。 Set
接口不提供根据索引获取项目的方法,因此对 Set
执行 .get(index)
会导致编译错误。请改用 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;
}
}
完成这些后,如果 ProviderValidator#isValid
returns false 只需执行 #fields.hasErrors('providers')
[=27= 即可轻松获得在 @ProviderValid
注释中指定的默认消息]