2
votes

Is it possible to enable default validation of fields without specifying group? For example, I have the bean:

class User {    
  @NotEmpty
  private String name;

  @NotEmpty(groups = UserGroup.ShouldHaveSurName.class)
  private String surname;
}

I want the field "name" to be validated in any case - if the group not specified for @Validated annotation in the controller, or if "ShouldHaveSurName" group specified. I believe there was the configuration for this but can't find it.

1

1 Answers

3
votes

From JSR-303 specification:

3.4. Group and group sequence

A group defines a subset of constraints. Instead of validating all constraints for a given object graph, only a subset is validated. This subset is defined by the the group or groups targeted. Each constraint declaration defines the list of groups it belongs to. If no group is explicitly declared, a constraint belongs to the Default group.

So doing following in controller should suffice:

@Validated({UserGroup.ShouldHaveSurName.class, Default.class})