0

In the current project I am working on the model bean has the phone number in 3 fields but we only would like one spring validation error. Can this be done>

@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;
SJS
  • 5,607
  • 19
  • 78
  • 105
  • You have two choices: 1) remove your validation from each of the phone number fields and instead create a [JSR-3030 class-level constraint](http://docs.jboss.org/hibernate/validator/4.3/reference/en-US/html_single/#d0e341) that can validate all the fields together. OR 2) as you are using Spring, you could code a [Spring Validator](http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#validator) for this. I would favour the JSR-303 constraint if you are prepared to do the dev. – sbk Mar 07 '13 at 15:32

1 Answers1

1

If it does not matter whenever the validation error occur at class level, but not at the field, then you can use hibernate validations ScriptAssert Validation

@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;
}

For more an other idea have a look at the crossfield validation discussions/questions. Cross field validation with Hibernate Validator (JSR 303)

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383