title: JSR303 - Apply all validation-groups defined in sequence
body: <p>I've got a bean I'd like to do conditional validation on. For this purpose, I've defined a <code>DefaultGroupSequenceProvider&lt;MyObject&gt;</code> which returns the list of groups to validate against.</p>
  
  <p>Now, when validating an object that violates the constraints in more than one group in the sequence, only the first group with failures return it's result. I'd like to get an error on all violations, not just the ones in the first group with failures.</p>
  
  <p>I'm thinking that this doesn't need a code-sample, but if I'm wrong, I'll be happy to provide one.</p>
  
  <p>I followed this <a href="http://kh-yiu.blogspot.com/2014/04/conditional-bean-validation-using.html" rel="nofollow noreferrer">http://kh-yiu.blogspot.com/2014/04/conditional-bean-validation-using.html</a> when creating the sequence. We use Spring if that matters. </p>
  
  <p>Just a note, this works, in that it is impossible that an invalid bean will be reported as valid. But if the user has some input that breaks 3 constraints, and I return back 2 failures, the user will get another failure on the last field as soon as the first ones are corrected. Not exactly user-friendly.</p>
  
  <p>Example:</p>
  
  <p>Bean</p>
  
  <pre><code>@GroupSequenceProvider(BeanSequenceProvider.class)
  public class MyBean {
      @NotEmpty
      private String name;
  
      @NotNull
      private MyType type;
  
      @NotEmpty(groups = Special.class)
      private String lastName;
  
      // Getters and setters        
  }
  </code></pre>
  
  <p>Enum type</p>
  
  <pre><code>public enum MyType {
      FIRST, SECOND
  }
  </code></pre>
  
  <p>Provider</p>
  
  <pre><code>public class BeanSequenceProvider implements DefaultGroupSequenceProvider&lt;MyBean&gt; {
      @Override
      public List&lt;Class&lt;?&gt;&gt; getValidationGroups(final MyBean object) {
          final List&lt;Class&lt;?&gt;&gt; classes = new ArrayList&lt;&gt;();
  
          classes.add(MyBean.class);
  
          if (object != null &amp;&amp; object.getType() == MyType.SECOND) {
              classes.add(Special.class);
          }
  
          return classes;
      }
  </code></pre>
  
  <p>Group annotation</p>
  
  <pre><code>public interface Special {
  }
  </code></pre>
  
  <p>Test class</p>
  
  <pre><code>public class MyBeanTest {
  
      private static Validator validator;
  
      private MyBean objectUnderTest;
  
      @BeforeClass
      public static void setUpOnce() throws Exception {
          final ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
  
          validator = validatorFactory.getValidator();
      }
  
      @Before
      public void setUp() throws Exception {
          objectUnderTest = new MyBean();
  
          objectUnderTest.setName("Simen");
          objectUnderTest.setType(MyType.FIRST);
          objectUnderTest.setLastName("Woop");
      }
  
      @Test
      public void testValid() throws Exception {
          assertThat(validator.validate(objectUnderTest), is(empty()));
      }
  
      @Test
      public void testMissingName() throws Exception {
          objectUnderTest.setName(null);
  
          assertThat(validator.validate(objectUnderTest), hasSize(1));
      }
  
      @Test
      public void testMissingLastName() throws Exception {
          objectUnderTest.setLastName(null);
  
          assertThat(validator.validate(objectUnderTest), is(empty()));
  
          objectUnderTest.setType(MyType.SECOND);
  
          assertThat(validator.validate(objectUnderTest), hasSize(1));
  
          objectUnderTest.setName(null);
  
          assertThat(validator.validate(objectUnderTest), hasSize(2));
      }
  </code></pre>
  
  <p>That very last assert fail, as there's one violation, not 2. As the constraint is violated in the default group, the <code>Special</code> group is not violated.</p>