16.3.5 Validation model
JSF technology provides a set of classes for validating input values entered into input components, such as text fields. These classes are called validators.
JSF offers some standard validators that you can use in your applications. Alternatively, you can write your own validator if none of the standard validators suits your needs.
Basically, a validator is an implementation class that checks an input value and sends an error message if the input is not valid. You use a validator by nesting it inside an input component whose input needs to be validated. If the validator decides that the user's input is invalid, the JSF FacesServlet redisplays the JSP page from which the form was submitted, without copying the local value to the Java Bean instance bound to the input component.
The JSF core tag library defines a set of tags that correspond to a set of standard classes for performing common data validation checks. Table 16-5 shows all the standard validation classes and corresponding tags.
Table 16-5
Validator Class Tag Function DoubleRangeValidator validateDoubleRange Checks whether the local value of a component is within a certain range. The value must be a floating-point or convertible to floating-point LengthValidator validateLength Checks whether the length of a component's local value is within a certain range. The value must be a java.lang.String LongRangeValidator validateLongRange Checks whether the local value of a component is within a certain range. The value must be any numeric type or String that can be converted to a longThe validator classes
To specify the range that the validators shown in Table 16-5 must check, you use the maximum and minimum attributes inside each of the validator's tag. These values can be hardcoded in the tag or refer to a backing-bean property.
You can also specify that an input component has a required attribute. By doing this, the JSF implementation checks whether the value of the component is null or is an empty String.
The validation model also allows you to create your own custom validator and corresponding tag to perform custom validation. There are two ways to implement custom validators:
![]()
Implement a Validator interface that performs the validation (see Example 16-11). By doing this, also:
- Register the Validator implementation with the application (See Example 16-12).
- Create a custom tag or use a validator tag to register the validator on the component (See Example 16-13).
![]()
Implement a backing-bean method that performs the validation. By doing this, also reference the validator from the component tag's validator attribute. Example 16-11 Implementing a validator
package itso.jsf.calculator;
import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.component.UIInput; import javax.faces.context.FacesContext; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException;
public class OddValidator implements Validator {
public void validate(FacesContext arg0, UIComponent arg1, Object arg2) throws ValidatorException { System.out.println("OddValidator start"); UIInput field = (UIInput)arg1; int value = ((Long)arg2).intValue(); System.out.println("Field="+field.getId()+" Value="+value); if (value%2 == 1) { field.setValid(true); System.out.println("OddValidator end: valid"); } else { System.out.println("OddValidator end: invalid"); FacesMessage errmsg = new FacesMessage (FacesMessage.SEVERITY_ERROR, "2nd number not odd.", "Second number must be odd."); throw new ValidatorException(errmsg); } } }
![]()
Example 16-12 Registering the validator
<validator> <description>Registers the OddValidator</description> <validator-id>oddValidator</validator-id> <validator-class>itso.jsf.calculator.OddValidator</validator-class> </validator>
![]()
Example 16-13 Invoking the validator with the validator tag
h:inputText styleClass="inputText" id="number2" required="true" maxlength="2" size="12" value="#{pc_Calculate.calculator.number2}"> <f:validator validatorId="oddValidator"></f:validator> </h:inputText>
![]()
ibm.com/redbooks