title: Hibernate-validator Groups with Spring MVC
body: <p>I'm using hibernate-validator 4.3.1 and Spring MVC 3.2.3.</p>
  
  <p>My application has a bean with the following properties and annotations (I've removed most of them to make it simple):</p>
  
  <pre><code>public class Account {
    @NotEmpty(groups = GroupOne.class)
    private String name;
  
    @NotEmpty(groups = GroupOne.class)
    private Date creationDate;
  
    @NotEmpty(groups = GroupTwo.class)
    private String role;
  
    @NotEmpty(groups = GroupTwo.class)
    private String profile;
  
    //getters and setters
  }
  </code></pre>
  
  <p>As it is showed, there are two groups of validations because the application has a wizard-form composed of two steps: in the first step the name and creationDate fields are filled by the user and in the second step the role and profile fields.</p>
  
  <p>I add some Controller methods as example:</p>
  
  <pre><code>@RequestMapping(value = "/account/stepOne", method = "POST")
  public String stepOne(@ModelAttribute @Validated(GroupOne.class) Account account, BindingResult bindingResult) {
    //Implementation
  }
  
  @RequestMapping(value = "/account/stepTwo", method = "POST")
  public String stepTwo(@ModelAttribute @Validated(GroupTwo.class) Account account, BindingResult bindingResult) {
    //Implementation
  }
  </code></pre>
  
  <p>The above methods specify which validation has to be applied by @Validated annotation.</p>
  
  <p>Until now everything works fine, but I have some problems when I want to reuse that bean in another form. This form displays all the fields of the bean and the controller's method which receives the submit is the following:</p>
  
  <pre><code>@RequestMapping(value = "/account/stepOne", method = "POST")
  public String stepOne(@ModelAttribute @Validated Account account, BindingResult bindingResult) {
    //Implementation
  }
  </code></pre>
  
  <p>As you can see, the name of the group has been removed from the @Validated annotation because I want to apply all the validations defined in Account bean.</p>
  
  <p>However, it didn't work and the only way to make it work has been to add the Default group to the bean's properties, as so:</p>
  
  <pre><code>public class Account {
    @NotEmpty(groups = {GroupOne.class, Default.class})
    private String name;
  
    @NotEmpty(groups = {GroupOne.class, Default.class})
    private Date creationDate;
  
    @NotEmpty(groups = {GroupTwo.class, Default.class})
    private String role;
  
    @NotEmpty(groups = {GroupTwo.class, Default.class})
    private String profile;
  
    //getters and setters
  }
  </code></pre>
  
  <p>Is there a more elegant way to achieve this?</p>