What You May Need to Know About Multiple Validators

Validating and Converting Input 6-15 about the Validator interface and FacesMessage, see the API documentation for javax.faces.validate.ValidatorException and javax.faces.application.FacesMessage, or visit http:download.oracle.comdocscdE17410_ 01javaeeindex.html . 3. If your application saves state on the client, your custom validator must implement the Serializable interface, or the StateHolder interface, and the saveStateFacesContext and restoreStateFacesContext, Object methods of the StateHolder interface. For more information, see the Javadoc for the StateHolder interface of the javax.faces.component package. 4. Register the validator in the faces-config.xml file. ■ Open the faces-config.xml file and select the Overview tab in the editor window. The faces-config.xml file is located in the View_ ProjectWEB-INF directory. ■ In the window, select Validators and click New. Click Help or press F1 for additional help in registering the validator. To create a client-side version of the validator: 1. Write a JavaScript version of the validator, passing relevant information to a constructor. 2. Implement the interface org.apache.myfaces.trinidad.validator.ClientValidator, which has two methods. The first method is getClientScript, which returns an implementation of the JavaScript Validator object. The second method is getClientValidation, which returns a JavaScript constructor that is used to instantiate an instance of the validator. Example 6–12 shows a validator in Java. Example 6–12 Java Validator public String getClientValidation FacesContext context, UIComponent component { return new SSNValidatorInvalid social security number.,Value \{1}\ must start with \123\.; } The Java validator calls the JavaScript validator shown in Example 6–13 . Example 6–13 Client-side JavaScript Validator function SSNValidatorsummary, detail { this._detail = detail; this._summary = summary; } To use a custom validator on a JSF page: ■ To use a custom validator that has a tag on a JSF page, you must manually nest it inside the component’s tag.