JSF Validation <f:validateBean> Tag
It is used to register a bean validator to the component. For validating bean model, you must set the context parameter in the web deployment descriptor file web.xml.
Bean Validation Constraints
JSF provides validation constraints for bean model in the form of annotations. You can place that annotations on a field, method, or class of a JavaBeans component, such as a managed bean.
JSF also provides facility to create custom or user defined constraints. The built-in constraints are available in the javax.validation.constraints package and tabled in the following table.
Built-In Bean Validation Constraints
Constraint | Description | Example |
---|---|---|
@NotNull | It is used to set not null constraint to the value of the field or property. | @NotNull String username; |
@Null | It is used set null constraint to the value of the field or property. | @Null String unusedString; |
@Size | It is used to specify size of field or property. The size of the field or property is evaluated and must match the specified boundaries. Use one of the optional max or min elements to specify the boundaries. | @Size(min=2, max=240) String briefMessage; |
@Digits | It is used to set constraint that the value of the field or property must be a number within a specified range. The integer element specifies the maximum integral digits for the number, and the fraction element specifies the maximum fractional digits for the number. | @Digits(integer=6, fraction=2) BigDecimal price; |
@DecimalMin | This constraint specifies that 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; |
@DecimalMax | It is used to specify that 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; |
@Max | It is used to set the value of the field or property which must be an integer value lower than or equal to the number in the value element. | @Max(10) int quantity; |
@Min | It is used to set the value of the field or property which must be an integer value greater than or equal to the number in the value element. | @Min(5) int quantity; |
@Pattern | It is used to set patter which must match the regular expression defined in the regexp element. | @Pattern(regexp=”\(\d{3}\)\d{3}-\d{4}”) String phoneNumber; |
@Past | It is used to set the value of the field or property which must be a date in the past. | @Past Date birthday; |
@Future | It is used to set the value of the field or property which must be a date in the future. | @Future Date eventDate; |
@AssertTrue | It is used to set the value of the field or property which must be true. | @AssertTrue boolean isActive; |
@AssertFalse | It is used to set the value of the field or property which must be false. | @AssertFalse boolean isUnsupported; |
// web.xml
This parameter value enables the JavaServer Faces implementation to treat empty strings as null.
// User.java
// index.xhtml
Output:
when submitting form this form validates for notnull validation.