21.1 Using Bean Validation Constraints
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. User-defined constraints are called custom constraints. Several built-in constraints are available in the javax.validation.constraints package. Table 21-1 lists all the built-in constraints. See Creating Custom Constraints for information on creating custom constraints.
Table 21-1 Built-In Bean Validation Constraints
| Constraint | Description | Example | 
|---|---|---|
| 
 
  | 
 The value of the field or property must be   | 
@AssertFalse boolean isUnsupported;  | 
| 
 
  | 
 The value of the field or property must be   | 
@AssertTrue boolean isActive;  | 
| 
 
  | 
 The value of the field or property must be a decimal value lower than or equal to the number in the value element.  | 
@DecimalMax("30.00")
BigDecimal discount;
 | 
| 
 
  | 
 The value of the field or property must be a decimal value greater than or equal to the number in the value element.  | 
@DecimalMin("5.00")
BigDecimal discount;
 | 
| 
 
  | 
 The value of the field or property must be a number within a specified range. The   | 
@Digits(integer=6, fraction=2) BigDecimal price;  | 
| 
 
  | 
 The value of the field or property must be a date in the future.  | 
@Future Date eventDate;  | 
| 
 
  | 
 The value of the field or property must be an integer value lower than or equal to the number in the value element.  | 
@Max(10) int quantity;  | 
| 
 
  | 
 The value of the field or property must be an integer value greater than or equal to the number in the value element.  | 
@Min(5) int quantity;  | 
| 
 
  | 
 The value of the field or property must not be null.  | 
@NotNull String username;  | 
| 
 
  | 
 The value of the field or property must be null.  | 
@Null String unusedString;  | 
| 
 
  | 
 The value of the field or property must be a date in the past.  | 
@Past Date birthday;  | 
| 
 
  | 
 The value of the field or property must match the regular expression defined in the   | 
@Pattern(regexp="\\(\\d{3}\\)\\d{3}-\\d{4}")
String phoneNumber;
 | 
| 
 
  | 
 The size of the field or property is evaluated and must match the specified boundaries. If the field or property is a   | 
@Size(min=2, max=240) String briefMessage;  | 
In the following example, a constraint is placed on a field using the built-in @NotNull constraint:
public class Name {
    @NotNull 
    private String firstname;
    @NotNull 
    private String lastname;
    ...
}
You can also place more than one constraint on a single JavaBeans component object. For example, you can place an additional constraint for size of field on the firstname and the lastname fields:
public class Name {
    @NotNull
    @Size(min=1, max=16)
    private String firstname;
    @NotNull 
    @Size(min=1, max=16)
    private String lastname;
    ...
}
The following example shows a method with a user-defined constraint that checks for a predefined email address pattern, such as a corporate email account:
@ValidEmail 
public String getEmailAddress() {
    return emailAddress;
}
For a built-in constraint, a default implementation is available. A user-defined or custom constraint needs a validation implementation. In the preceding example, the @ValidEmail custom constraint needs an implementation class.
Any validation failures are gracefully handled and can be displayed by the h:messages tag.
Any managed bean that contains Bean Validation annotations automatically gets validation constraints placed on the fields on a JavaServer Faces application's web pages.
For more information on using validation constraints, see the following:
