Java Reference
In-Depth Information
@Size(max = 30)
private String firstname;
@Size(max = 30)
private String lastname;
@Valid//To propagate the validation of a
student object
private Address address;
//getter and setter
}
public class Address {
@NotNull(groups=AddressCheck.class)
@Size(max = 10,groups=AddressCheck.class)
private String phone;
@NotNull(groups=AddressCheck.class)
@Email(groups=AddressCheck.class)
private String email;
//getter and setter
}
public interface AddressCheck { }
To enable the validation of an object step-by-step, the Bean Validation Specification
proposes the notion of groups. This gives you the ability to define a subset of con-
straints that can be validated separately. By default, validation constraints belongs to
the
Default
group. And if a validation group is not specified when validating data,
only the constraints of the
Default
group will be checked. This justifies the fact that
the code of the
testDefaultGroup()
method will run entirely without errors. Al-
though the phone and the e-mail attributes of the
Address
class are not conformed
to the constraints, they will not be validated for the simple reason that the constraints
that decorate them are not a part of the
Default
group. This can be seen in the
following code:
public void testDefaultGroup(){
ValidatorFactory factory