ManojKumar S
ManojKumar S

Reputation: 11

Hibernate validations annotations order priority

public class AddressDTO {
   @NotNull
   @NotBlank 
   @Pattern(regexp="(0|91)?[789][0-9]{9}")
   String phoneNumber;
}

How can i provide priority validations for these DTO class by using Hibernate annotations??

Upvotes: 1

Views: 2067

Answers (1)

Guillaume Smet
Guillaume Smet

Reputation: 10539

If you want to validate the constraints in order and only fail for the first failed constraint, you need to use groups and groups sequences.

See this paragraph of the documentation on how to use group sequences: http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-defining-group-sequences .

It's going to be a bit verbose, though.

Another possibility depending on your requirements would be to use a constraint composition and @ReportAsSingleViolation . See http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-constraint-composition .

And the last one (already mentioned) would be to write a custom constraint with your own validation code verifying your "constraints" in order and adapting the error message depending on what you check.

Upvotes: 2

Related Questions