Adding ADF Faces Validation

6-12 Web User Interface Developers Guide for Oracle Application Development Framework To add ADF Faces validators: 1. In the Structure window, right-click the component for which you would like to add a validator.

2. In the context menu, choose Insert inside UI component, then ADF Faces to

insert an ADF Faces validator, or JSF Core to insert a JSF reference implementation validator.

3. Choose a validator tag for example, ValidateDateTimeRange.

4. In the Property Inspector, set values for the attributes, including any messages for validation errors. For additional help, right-click any of the attributes and choose Help . ADF Faces lets you customize the detail portion of a validation error message. By setting a value for a MessageDetailxyz attribute, where xyz is the validation error type for example, MessageDetailmaximum, ADF Faces displays the custom message instead of a default message, if validation fails.

6.5.2 What Happens at Runtime

When the user submits the page, ADF Faces checks the submitted value and runs conversion on any non-null value. The converted value is then passed to the validate method. If the value is empty, the required attribute of the component is checked and an error message is generated if indicated. If the submitted value is non-null, the validation process continues and all validators on the component are called in order of their declaration. ADF Faces validation is performed during the Process Validations phase. If any errors are encountered, the components are invalidated and the associated messages are added to the queue in the FacesContext instance. Once all validation is run on the components, control passes to the model layer, which runs the Validate Model Updates phase. As with the Process Validations phase, if any errors are encountered, the components are invalidated and the associated messages are added to the queue in the FacesContext instance. The lifecycle then goes to the Render Response phase and redisplays the current page. ADF Faces automatically displays an error icon next to the label of any input component that generated an error, and displays the associated messages in a popup window unless the af:message component inline attribute is set to true. Figure 6–2 shows a server-side validation error. RegExpValidator af:validateRegExp Validates the data using Java regular expression syntax. Note: To register a custom validator on a component, use a standard JSF f:validator tag. For information about using custom validators, see Section 6.6, Creating Custom JSF Validation. Note: ADF Faces provides extensions to the standard JSF validators, which have client-side support. Table 6–3 Cont. ADF Faces Validators Validator Tag Name Description Validating and Converting Input 6-13 Figure 6–2 Server-Side Validation Error

6.5.3 What You May Need to Know About Multiple Validators

You can set zero or more validators on a UI component. You can set the required attribute and use validators on a component. However, if you set the required attribute to true and the value is null or a zero-length string, the component is invalidated and any other validators registered on the component are not called. This combination might be an issue if there is a valid case for the component to be empty. For example, if the page contains a Cancel button, the user should be able to click that button and navigate off the page without entering any data. To handle this case, you set the immediate attribute on the Cancel button’s component to true. This attribute allows the action to be executed during the Apply Request Values phase. Then the default JSF action listener calls FacesContext.renderResponse, thus bypassing the validation whenever the action is executed. For more information see Chapter 4, Using the JSF Lifecycle with ADF Faces.

6.6 Creating Custom JSF Validation

You can add your own validation logic to meet your specific business needs. If you want custom validation logic for a component on a single page, you can create a validation method on the page’s backing bean. If you want to create logic that will be reused by various pages within the application, or if you want the validation to be able to run on the client side, you should create a JSF validator class. You can then create an ADF Faces version, which will allow the validator to run on the client.

6.6.1 How to Create a Backing Bean Validation Method

When you want custom validation for a component on a single page, create a method that provides the required validation on a backing bean. To add a backing bean validation method: 1. Insert the component that will require validation into the JSF page.

2. In the visual editor, double-click the component to open the Bind Validator

Property dialog.

3. In the Bind Validator Property dialog, enter or select the managed bean that will

hold the validation method, or click New to create a new managed bean. Use the default method signature provided or select an existing method if the logic already exists. When you click OK in the dialog, JDeveloper adds a skeleton method to the code and opens the bean in the source editor.