2

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.

Minar Mahmud
  • 2,577
  • 6
  • 20
  • 32
Evgenii
  • 447
  • 1
  • 10
  • 26
  • Does this answer your question? [JSR-303 validation groups define a default group](https://stackoverflow.com/questions/35358447/jsr-303-validation-groups-define-a-default-group) – M. Justin Apr 22 '22 at 20:57

1 Answers1

8

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})
Minar Mahmud
  • 2,577
  • 6
  • 20
  • 32