Overriding default ADF validation messages

Overriding default ADF validation messages

Gepubliceerd: Categorie: Oracle

This article describes several ways to override default ADF validation messages. Basic component level message overriding is an easy to use feature of ADF. It has however some limitations. These limits can be overcome by using global application level overriding.
 

The built-in ADF validators provide us with means to validate user input. It enables us, without coding to validate:

  • Ranges of numbers and dates
  • The length of fields
  • Inputs based on regular expressions

These built-in validators perform their checks client-side (in JavaScript), so no need for a server-client roundtrip.

But what if we need to customize the default validation messages and hints, which are built into ADF? 

Fig 1: Different ADF validators in action using default validation messages

Say we need to override the Double Range validation message: “The number must be between 4 and 20”(as depicted in figure 1).

Component Level: Validator Message Overriding

Validation message overriding is available as a property on the validator component level. For each separate input UI component on the page one can override the validator’s messages.

Take for instance the Double Range Validator: <af:validateDoubleRange>

It is possible to modify both the message and hint. Using the validator properties we can change the validation messages to suit the user’s needs. Figure 2, 3 and 4 show how to override messages in detail.

Fig 2: Property Inspector for the ValidateDoubleRange component
Fig 3: Corresponding code for this ValidateDoubleRange component
  1. <af:inputText label="Double Range" id="it1">
  2. <af:validateDoubleRange minimum="4.0" maximum="20.0"
  3. hintNotInRange="Please provide a value between {0} and {1}"
  4. messageDetailNotInRange="{0}: You have entered {1} which is not between {2} and {3}. Please provide a different value"/>
  5. </af:inputText>
Fig 4: Runtime result of overriding various validation messages

Figure 4 shows us how grouped validation messages are rendered. Figure 5 shows a single validation message belonging to one of the components.

Fig 5: Validator message popup per component

Please take note of the highlighted messagebox title in figure 5. This part of the messagebox cannot be changed using the validator properties. Simply because the available properties on the validator do not include the message title.

To be able to also modify this messagebox title, please read on…

Global Validator Message Overriding

Instead of overriding the validation messages per validator component, it is possible to override a specific message for the entire application.

How?

1) Create a property file
2) Configure faces-config.xml to use this property file as a message bundle
3) Overriding the seeded validation message in the property file
4) Clear browser cache & run application

In this section we will override the title for the af:validateLength validator.

1. Create property file
First we have to create a property file.
In JDeveloper: File > New > File >
Select as filename CustomMessageBundle.properties
The folder can be something like: ViewController\src\com\qualogy\view

Fig 6: Create file dialog

The new property file will open up automatically. Here we can override ADF’s seeded validation messages.

2. Configure Message Bundle
Now we need to tell ADF where our custom message bundle is located.

1) Open faces-config.xml
2) Select the overview tab
3) Select ‘Application’
4) Add our newly created property file as the Message Bundle: com.qualogy.view.CustomMessageBundle

Fig 7: Configuration of message bundle in faces-config.xml

3. Overriding seeded validation message
To actually override the validation message we first need to find the proper message key for this message type. All ADF Validator message types can be found in Oracle’s Fusion Middleware Web User Interface Developer’s Guide.

For component af:validateLength we find message keyorg.apache.myfaces.trinidad.validator.LengthValidator.MINIMUM_details
The corresponding title message key is: org.apache.myfaces.trinidad.validator.LengthValidator.MINIMUM

We add both message keys to our message bundle defined in CustomMessageBundle.properties

  1. org.apache.myfaces.trinidad.validator.LengthValidator.MINIMUM=Custom Validation Error: Minimum length not reached!
  2. org.apache.myfaces.trinidad.validator.LengthValidator.MINIMUM_detail={0}: Custom Validation The input value {1} is less than the mimimum allowed ({2})
  3.  

4. Clear cache and run Application
It is time to test our application. Clear your browser cache and run the application.

Please mind that ADF validation messages are stored in the browser cache (JavaScript). By clearing the browser cache, we enforce a download of the updated ADF Messages JavaScript.

We now see the overridden message as defined on the component.

Fig 8: Runtime example showing validation messagebox title overridden by a message bundle

Please note that the messagebox title contains the value from our message bundle: “Error: Custom Validation Error: Minimum length not reached!”

Also note the validation message details are taken from the validation component property first. This property has precedence over any value defined in our custom message bundle.

Multilanguage support

Our previous message bundle merely overwrites the message for the default language (English). Message bundles also allow overriding messages for other languages.

How?

  • Create a new property file for each language to be overridden. The name should be the same as the message bundle property file defined earlier, except that it needs to have a country code suffix.For Dutch this will be: CustomMessageBundle_nl.properties
  • You need to override the same message keys in the language specific property files for this to work. e.g.: org.apache.myfaces.trinidad.validator.LengthValidator.MINIMUM=Maatwerk validatie fout: Minimum aantal karakters niet ingevuld!
  • Add the supported locales to the faces-config.xml
  1. <?xml version="1.0" encoding="windows-1252"?>
  2. <faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee">
  3. <application>
  4. <default-render-kit-id>oracle.adf.rich</default-render-kit-id>
  5. <message-bundle>com.qualogy.view.CustomMessageBundle</message-bundle>
  6. <locale-config>
  7. <default-locale>nl</default-locale>
  8. <supported-locale>en</supported-locale>
  9. <supported-locale>nl</supported-locale>
  10. </locale-config>
  11. </application>
  12. </faces-config>
  • For testing purposes we override the default-locale to: nl
  • Instead of setting a default locale, you can also configure the browser to use a different language.

The result is a translated message title: “Fout: Maatwerk validatie fout: Minimum aantal karakters niet ingevuld!” (see figure 9).

Fig 9: Validation message using localized message bundle

Please note the message detail remains in English, as defined on the validator. Maybe it’s time to use a Resource Bundle for translating these messages as well! I won’t dive into multi-language Resource Bundles. These are already well described elsewhere on the web.

Source code can be found here: ADFValidationMessages

Known Bugs

The DoubleRangeValidator does not actually use it’s own message keys. It uses the messages defined for the LongRangeValidator instead. So instead of overriding the DoubleRangeValidator message keys we need to override the LongRangeValidator keys instead.

e.g.:

  1. org.apache.myfaces.trinidad.validator.LongRangeValidator.NOT_IN_RANGE=Custom Validation: messagebox title telling the user the value is not in range
  2. org.apache.myfaces.trinidad.validator.LongRangeValidator.NOT_IN_RANGE_details=Custom validation message detail telling the number {1} is not between {2} and {3}

Custom Validators

You can also define your own validators, handling the entire error handling messages yourself. Please check other blogs online for more details. One of these can be found on: http://www.awasthiashish.com/2012/11/custom-validator-in-oracle-adf-jsf_20.html

Sources

Fusion Middleware Web User Interface Developer’s Guide
Custom Validator in Oracle ADF-JSF

Richard Velden
Over auteur Richard Velden

Oracle Fusion Middleware Developer at Qualogy. Specializes in integration and cloud development using Oracle technologies such as: SOA Suite, Service Bus, Integration and Process Cloud.

Meer posts van Richard Velden
Reacties (1)
  1. om 22:10

    Hi Richard,
    It was nice blog about ADF validation. I have a doubt that the message being displayed after the validation fails shall be lost once the focus is lost. To display the error message again we need to place the focus on the respective component again. Instead, Is there any chance of displaying it constantly until we make the field valid???

Reactie plaatsen