Java Reference
In-Depth Information
Validation groups are Java interfaces, so to create a group, you simply create an interface. For instance,
to create the authorization group that was mentioned in the previous paragraph, an interface such as the following
could be created:
public interface Authentication {}
To apply one or more groups to a specified constraint annotation, use the groups attribute of the annotation
constraint and indicate the class name of the interface. For instance, in the following example, the User class contains
two fields that belong to the Authentication group:
public class Users {
@Size(min=8, groups=Authentication.class)
private String username;
@Size(min=8, groups=Authentication.class)
@Pattern(regexp="^[a-zA-Z\\d_]{4,12}$")
private String password;
@NotNull
private String firstname;
@NotNull
private String lastname;
@NotNull()
private String email;
...
}
It is possible to specify more than one group for a constraint annotation by denoting a list of values for the groups
attribute. For instance, if the username field needed to also belong to the Authentication2 group, then it could be
registered to both groups using the following notation:
@Size(min=8, groups={Authentication.class, Authentication2.class})
private String username;
the Default.class group is created automatically, and any validation constraint that does not have a group
specified will belong to the Default group.
Note
Group Validation
In some situations, it makes sense to have one group inherit another. In such cases, it is possible to apply group
inheritance by performing interface inheritance. For example, if the Authentication group needs to inherit the
BaseAuthentication group, it could be done by specifying the inheritance within the Authentication interface.
public interface Authentication extends BasicAuthentication {}
 
 
Search WWH ::




Custom Search