1

Say I have validation on a field like this:

@NotEmpty @Digits(integer = 3, fraction = 0) private String code; 

Using Spring MVC and Hibernate validation currently I get both messages if I leave the form field blank. Is there a way to only display the @NotEmpty message?

1 Answer 1

6

If you want to stick to the Bean Validation Specification you need to use a group sequence. Only a groups sequence guarantees an ordered constraint evaluation which stops on the first error. Something like:

@GroupSequence({ First.class, Second.class }) public class MyBean { @NotEmpty(groups = First.class) @Digits(integer = 3, fraction = 0, groups = Second.class) private String code; } 
Sign up to request clarification or add additional context in comments.

2 Comments

If validating just using the default group sequence and getting "javax.validation.GroupDefinitionException: MyBean must be part of the redefined default group sequence" make sure to include the bean in the @GroupSequence, i.e.: @GroupSequence({ First.class, Second.class, MyBean.class }).
Simple yet efficient!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.