Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bean Validation Groups - Understanding it correctly

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.

like image 548
Kevin Rave Avatar asked Jul 04 '13 17:07

Kevin Rave


People also ask

How can you explain Bean Validation?

JavaBeans Validation (Bean Validation) is a new validation model available as part of Java EE 6 platform. The Bean Validation model is supported by constraints in the form of annotations placed on a field, method, or class of a JavaBeans component, such as a managed bean. Constraints can be built in or user defined.

How does Java Bean validation work?

Java Bean Validation, also known as JSR-303 (Java Specification Requests), is an annotation based validation specification for validating fields on objects. Java Bean Validation uses flexible annotations, which can be overridden or extended through the Java code or in an XML file, to apply validation constraints.

What is validation group in Java?

Validation constraints in Bean Validation may be added to one or more groups via groups attribute. This allows you to restrict the set of constraints applied during validation. It can be handy in cases where some groups should be validated before others like e.g. in wizards.

What are validation groups?

Validation groups allow you to organize validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page. You create a validation group by setting the ValidationGroup property to the same name (a string) for all the controls you want to group.


1 Answers

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).

like image 165
Arnaud Denoyelle Avatar answered Oct 04 '22 15:10

Arnaud Denoyelle