2013-03-07 20 views
0

在当前项目中,我在模型bean上的电话号码在3个字段中,但我们只想要一个弹簧验证错误。可以这样做>电话号码是在春天模型中的3个字段,但我只想要一个验证错误

@NotEmpty(message = "Your Question must not be blank.") 
@Size(min = 10, max = 200) 
private String content; 

@NotEmpty(message = "Area Code must not be blank.") 
@Size(min = 3, max = 3, message = "Area must be 3 numbers") 
private String areacode; 

@NotEmpty(message = "Phone Number must not be blank.") 
@Size(min = 3, max = 3, message = "phone number first part must be 3 numbers") 
private String phone3; 

@NotEmpty(message = "Phone Number must not be blank.") 
@Size(min = 4, max = 4, message = "Phone number last part must be 4 numbers") 
private String phone4; 
+0

您有两种选择:1)从每个电话号码字段中删除您的验证,而是创建一个[JSR-3030类级约束](http://docs.jboss.org/hibernate/validator/ 4.3/reference/en-US/html_single /#d0e341)可以一起验证所有字段。或者2),因为你使用的是Spring,你可以编写一个[Spring Validator](http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference)。 html#validator)。如果您准备开发开发工具,我会倾向于使用JSR-303约束。 – sbk 2013-03-07 15:32:18

回答

1

如果每当验证错误发生在类级别不要紧,但不能在外地,那么你可以使用Hibernate的验证ScriptAssert验证

@ScriptAssert(
    lang="javascript" 
    script="_this.areacode!=null and _this.phone3!=null and _this.phone4!=null" 
) 
public class YourClass { 
    private String areacode; 
    private String phone3; 
    private String phone4; 
} 

更多的其他想法看看跨领域验证讨论/问题。 Cross field validation with Hibernate Validator (JSR 303)