10.5.1 Procedures for using Bean Validation from JSF

This subsection describes procedures for using Bean Validation from JSF.

Organization of this subsection
(1) Preconditions for use
(2) Validation operations
(3) Example of implementation

(1) Preconditions for use

The following conditions must be satisfied to perform the validation processing using Bean Validation in JSF:

(2) Validation operations

The validation operations of the Bean Validation function vary depending on the value of the context parameter provided by JSF. The operations change as described in the following table depending on the value specified for javax.faces.validator.DISABLE_DEFAULT_BEAN_VALIDATOR and the presence or absence of the <f:validateBean> tag for the ManagedBean variable that defines the annotation for Bean Validation.

Table 10-4 Relationship of the value specified for javax.faces.validator.DISABLE_DEFAULT_BEAN_VALIDATOR and the validation processing

Value specified for javax.faces.validator.DISABLE_DEFAULT_BEAN_VALIDATORInput value
Specification of the <f:validateBean> tag (when disabled attribute is not specified)
Validation by Bean Validation
trueYesYes
NoNo
falseYesYes
NoYes
If value is not specified (default)Same as when false is specifiedSame as when false is specified

For details on how to use the <f:validateBean> tag, see the standard specifications for JSF. For details on how to define validation for the ManagedBean variable, see the standard specifications for Bean Validation.


(3) Example of implementation

The following is an example implementation of using Bean Validation from JSF.

The first example shows an implementation of the Facelets page that registers the information for which validation is required.

<f:view>
 <h:form>
   Enter ID <br/>
   <h:inputText id="IDBox" value="#{personalData.id}" /><br/>
   <h:message for="IDBox"/><br/>
   <br/>
  <f:validateBean disabled="true">
   Enter name (validation processing is not performed)<br/>
   <h:inputText id="NameBox" value="#{personalData.name}" /><br/>
  </f:validateBean >
   <br/>
   Enter age<br/>
   <h:inputText id="AgeBox" value="#{personalData.age}" /><br/>
   <h:message for="AgeBox"/><br/>
   <br/>
  <br/>
   ...
   ...
 </h:form>
</f:view>

Note
The <inputText> tag that specifies <f:validateBean disabled="true"> defines validation for the corresponding ManagedBean variable, but the validation processing is not performed.

The next example shows an implementation of the validation definition for the ManagedBean variable that stores the data to be validated.

@ManagedBean(name="personalData")
@SessionScoped
public class PersonalData
{
 @Size(min=8,max=12, message="Enter a string of 8 to 12 characters.")
 private String id = "";

 @Size(min=1,message="Enter the name.")
 private String name = "";

 @Max(value = 150, message="Check whether the age is entered correctly.")
 @Min(value = 0, message="Enter an age of at least 0 years.")
 private int age = 0;
  ...
setter/gettter method
  ...
}