Friday, October 8, 2010

Create custom validators in ADF application


If somehow you need to create your own validation logic to meet your business needs. You can either create a validation method on the page's backing bean (if you want custom validation for a component on a single page), or create JSF validator classes (if you want to reuse the validation logics by various pages in the application).
In the real world, of course creating the custom JSF validator classes would be adopted for ADF application for better reuse and maintantence.
Here I present an example on creating custom validators in ADF application. In the example, I created two validators, one for email validation and the other for dateMustComeAfter valiation. You can add as many as you need to meet your requirements.
1. Create a new application based on ADF template.
2. In the viewController layer, create two validators classes - one called "EmailValidator" - to validate a valid email address - and the other called "DateMustComeAfterValidator" - to validate one date input has to be come after the other.

Here is the codes snapshots:

EmailValidator:


DateMustComeAfterValidator:





















In the java codes, please note you have to contain a validate method to implement Validator interface. The validate method has three arguments (FacesContext, UIComponent and Object). The UIComponent represent the component to be validated, and the object represent the value of the component to be validated.

3. Register the custom validator in faces-config.xml file. Open faces-config.xml, on the validator tab, click new to add the emailValidator and dateValidator.
























4. Add the validators to the components. Create a TestValidator page to test validations. In the page, drag a inputText and two inputDate component.

Right click on the inputText and choose "insert inside' --> JSF core --> validator. In the validator attribute, choose the validatorId as the name of the validator we registered in faces-config.xml file, it is "custom.EmailValidator". Perform same actions on the inputDate component. The difference in the inputDate component is that you have to add an "attribute" for the validator. The name of the attribute is "StartDateCompoonentId" we have used in the DateMustComeAfterValidator java file and the value is the component id of the first inputDate component.



Here are the user interface snapshots:

Email Validation:















Date Validation: 


















You can download the application if you wish to know more details: CustomValidatorApp.zip

The Web User Interface Developer's Guide demonstrated the usage and customizations of ADF converters and validators.
http://download.oracle.com/docs/cd/E14571_01/web.1111/b31973/af_validate.htm#ADFUI343

4 comments:

Bali said...

Awesome , Thanks.

SJK said...

Your blog is great. Thank you very much

Steve said...

Do you know how to prevent the message from displaying twice? Better, how to display a short message at the top in bold, where it says "Error:"; and then a detailed message below it in normal type?

Gnanaprakash said...

Thank you