How to Create a Backing Bean Validation Method

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. 6-16 Web User Interface Developers Guide for Oracle Application Development Framework Example 6–14 shows a custom validator tag nested inside an inputText component. Note that the tag attributes are used to provide the values for the validator’s properties that were declared in the faces-config.xml file. Example 6–14 A Custom Validator Tag on a JSF Page h:inputText id=empnumber required=true hdemo:emValidator emPatterns=9999|9 9 9 9|9-9-9-9 h:inputText To use a custom validator without a custom tag: To use a custom validator without a custom tag, nest the validator’s ID as configured in faces-config.xml file inside the f:validator tag. The validator’s ID attribute supports EL expression such that the application can dynamically determine the validator to use. 1. From the Structure window, right-click the input component for which you want to add validation, and choose Insert inside component ADF Faces Core Validator .

2. Select the validator’s ID from the dropdown list and click OK.

JDeveloper inserts code on the JSF page that makes the validator ID a property of the f:validator tag. Example 6–15 shows the code on a JSF page for a validator using the f:validator tag. Example 6–15 A Custom Validator Nested Within a Component on a JSF Page af:inputText id=empnumber required=true f:validator validatorID=emValidator af:inputText

6.6.4 What Happens When You Use a Custom JSF Validator

When you use a custom JSF validator, the application accesses the validator class referenced in either the custom tag or the f:validator tag and executes the validate method. This method accesses the data from the component in the current FacesContext and executes logic against it to determine if it is valid. If the validator has attributes, those attributes are also accessed and used in the validation routine. Like standard validators, if the custom validation fails, associated messages are placed in the message queue in the FacesContext instance.