How to Create a Custom JSF Validator

Environment (JDeveloper 11.1.1.5.0, ADF BC, ADF Faces, hr schema)

ADF Faces provides several validation types; UI component attributes validation, default ADF Faces validators, and custom ADF Faces validators. The last type (custom ADF Faces validators) enables you to create your own validation logic in a Java class then you can use it as a validator to your UI component. The custom validator can be created for a specific component in a single page, or can be created to be used in all pages in your application. In this example, I will explain how to build your own custom validator that can be used in all pages. This example based on hr schema, mainly on Employees table, and assumed that you already have built your BC. Our business rule is that the PHONE_NUMBER attribute should be at least 9 digits. The steps to build your own validator are:

  1. Create a Java class that implements the javax.faces.validator.Validator
    interface in your viewController project as shown below. In the name field enter “PhoneValidator”, enter your desired package, and in the Optional Attributes section click the green plus symbol to add the interface that this class should implement.

    PhoneValidator class

  2. After you have created the class, you will see the validate method to implement the interface. override this method as shown below.
    public void validate(FacesContext facesContext,
    UIComponent uIComponent,
    Object object)
    throws ValidatorException {
    //add your validation logic here
    String phone = object.toString();
    if (phone.length() < 9) {
    FacesMessage fm =
    new FacesMessage(“Phone number should be at least 9 digits”);
    throw new ValidatorException(fm);
    }
    }
  3. Now you need to register your validator in faces-config.xmlfile, locate this file in your viewController project then double click on it, after the file was opened in your main window click the overview tab, then select the validator link from the left hand side. Click on the green plus symbol to add your validator as shown below.

    register the Phonevalidator

  4. Now our validator is ready to use, create a page, drag and drop the EmployeeView1 from the data control, and select ADF form from the menu.
  5. Right click on the PhoneNumbe filed, and select insert inside Input Text → JSF Core → Validator. Once you add the validator, go to the property inspector palette, select the ValidatorId property then select our PhoneValidator from the list. your page source should look like this source snapshot.
    <af:inputText value=”#{bindings.PhoneNumber.inputValue}”
    label=”#{bindings.PhoneNumber.hints.label}”
    required=”#{bindings.PhoneNumber.hints.mandatory}”
    columns=”#{bindings.PhoneNumber.hints.displayWidth}”
    shortDesc=”#{bindings.PhoneNumber.hints.tooltip}”
    id=”it4″>
    <f:validator binding=”#{bindings.PhoneNumber.validator}”/>
    <f:validator validatorId=”phonevalidator”/>
    </af:inputText>
  6. Run your page and enter a phone number less than 9 digits, submit the form, the error message will be shown as below.

    error message shown after violate PhoneValidator

see also: Add business rule in the setter method of an entity attribute

Advertisements

9 Comments

Filed under ADF

9 responses to “How to Create a Custom JSF Validator

  1. sdoulger

    Nice post!
    One question though. What is the difference with the way presented below:
    1.Add attribute in your af:inputText,
    validator=”#{your_Backing.validator}”.
    2.In your page backing, add method:
    public void validator(javax.faces.context.FacesContext facesContext,
    UIComponent uIComponent, Object object) {

    throw new ValidatorException(“error messages”);
    }
    3. No need to add it in faces-config.xml. Ony the backing need to be declared in the TF.
    One difference that I can see is that in your case you can declare multiple validators.
    Thanx in advance

    Spyros Doulgeridis
    adfhowto.blogspot.com

    • 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 as you mentioned.But, If you want to create logic that will be
      reused by various pages within the application, you should create a JSF validator class.

  2. sorry….
    Is a answer …
    I can validate two fields of the same EO. ???
    Greetings.

    as can be done?
    Can you give me an example. please me.

  3. Pingback: Oracle ADF custom Validator for BSN check « Jan van Zoggel

  4. diegol

    Hi can I add more methods to this class? or I have to create only one validator method by each class

  5. Amber

    How can i implement the validation on field entered in datatable , which should prompt user saying value entered is already existing , enter the unique value . This behaviour should be done at the time of tab out

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s