Hibernate验证上的自定义错误消息 [英] Custom error messaging on Hibernate Validation
问题描述
我正在尝试在我的电子商务站点中设置休眠验证. 我有一个分配了多个对象的订单对象.当客户进行结帐时,我希望能够单独验证这些对象-有时以一种形式验证多个对象.
I am trying to set up Hibernate Validation in my ecommerce site. I have an order object with multiple objects assigned. As the customer goes through the checkout, I want to be able to individually validate these objects - sometimes multiple objects with one form.
例如,在提交交货表单后,应该验证deliveryCharge和deliveryAddress.如果此验证失败,则将返回交付表单,其中包含验证错误列表.
For example, upon submitting the delivery form, the deliveryCharge and deliveryAddress should be validated. If this validation fails, the delivery form will be returned with a list of validation errors.
我可以通过Java实现来验证对象,但是当我尝试使用<form:error />
标签在视图层上查看这些对象时,我什么也没得到.
I can validate the objects via a java implementation, however when I try to view these on the view tier using <form:error />
tag, I am not getting anything.
订单模型
@Entity
@Table(name = "bees_address")
public class Address {
@OneToOne
@JoinColumn(name = "paymentAddress")
private Address payment;
@OneToOne
@JoinColumn(name = "deliveryAddress")
private Address payment;
@Column(name = "deliveryCharge")
private Integer deliveryCharge;
...
地址模型
@Entity
@Table(name = "bees_address")
public class Address {
@Size(min=2, max=150)
@Column(name = "line1", length = 150)
private String line1;
...
控制器
public String updateDelivery(HttpServletRequest request, @ModelAttribute("basket") Order basketUpdate) {
Address deliveryAddress = basketUpdate.getDeliveryAddress();
if (!Validate.isValid(request, deliveryAddress)) {
logger.info("Delivery address does not validate");
return "redirect:/checkout/delivery";
} else {
/* do stuff here */
}
return "redirect:/checkout/payment";
}
验证 休眠验证文档
public static Boolean isValid(HttpServletRequest request, Address address) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Address>> constraintViolations = validator.validate(address);
request.getSession().setAttribute("formErrors", constraintViolations);
return constraintViolations.size() < 1;
}
JSP结构
<form:form action="${url}" method="post" modelAttribute="basket"
Charge: <form:input path="deliveryCharge" />
Address: <form:input path="deliveryAddress.line1" />
<form:error path="deliveryAddress.line1" />
...
非常感谢
推荐答案
我认为您需要的是验证组.它们使您可以通过几个步骤来验证不同的约束集.
I think what you are after is validation groups. They allow you to validate different sets of constraints in several steps.
通过定义接口来声明所需的组,并将您的约束分配给一个或多个组:
Declare the required groups by defining interfaces and assign your constraints to one or more groups:
public interface DeliveryChecks{}
public interface PaymentChecks{}
public class Address {
@NotNull(groups = PaymentChecks.class)
private Address payment;
@Min(value=5, groups = DeliveryChecks.class)
private Integer deliveryCharge;
...
}
然后通过将组标识符传递给validate()
来验证每个表单或页面所需的组:
Then validate the required group(s) for each form or page by passing the group identifiers to validate()
:
Set<ConstraintViolation<Address>> constraintViolations =
validator.validate(address, PaymentChecks.class);
Hibernate验证程序参考指南有关此主题的更多信息.
The Hibernate Validator reference guide has more information on this topic.
这篇关于Hibernate验证上的自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!