I am trying to understand the Groups in Bean validation.
So for instance if I have a bean and I want only certain field validated for certain cases, I should group them?
@NotNull (groups=MyClassX.class)
@Min (groups=MyClassA.class) // 1
@Pattern(xxxxx, groups = MyClassA.class) // 2
private String field1;
@NotNull (groups=MyClassX.class)
@Min (groups=MyClassX.class)
@Pattern(xxxxx, groups=MyClassX.class))
private String field2;
@NotNull (groups=MyClassX.class)
@Min (groups=MyClassX.class)
@Pattern(xxxxx, groups=MyClassA.class) //3
private String field3;
My understanding from the above example is, if I pass MyClassA
to validator, then only @Min
and @Pattern
for Field1
and @Pattern
for field3
are only validated? (marked with numbers 1,2 and 3)
Did I understand this correctly?
I haven't left any fields without Groups
attribute. So no default group.
First, here is the javax.validation javadoc
When you want to validate a bean, you actually call Validator.validate(T object, java.lang.Class... groups)
It will then check the validations constraints of the specified groups. It allows to use several validation cases.
What you describe in your question is accurate.
Note, if you do not put any group on your constraints, then the constraints belong to the default group, which is the only validated group if you don't specify any group when calling validate(T object).