Wednesday, September 17, 2014

Using ADF BC Declarative Built-in Rules (Part 6 of 10): List validator

The "List Validator" is yet another validator that can be defined at either the entity level or the attribute level but pertains to an entity object attribute to ensure that a value is either in a list or not.

So the "List Validator" compares an attribute against a list of

a) literal values ensuring that the value is in or not in the list of literal values that you define,
b) against an SQL query ensuring that the value is in or not in the first column of the query's result set,
c) against a view attribute ensuring that the value is in or not in the attribute of the specified view object or
d) against a view accessor ensuring that the value is in or not in the specified attribute in all rows of the view object retrieved by the view accessor.

Let's see a demo of the "List Validator". I have created a new ADF Fusion Web Application and created the basic business components that I will be using in this demo, an entity object based on the Departments HR table, a view object based on the Departments entity object and a default application module.

Next let’s define a "List" validator on the Departments entity object to ensure that a department is in the list of literal values that we will define. So on “Business Rules” tab of the Departments entity object, click on the green plus icon “Create new validator”.  This will open the “Add Validation Rule” editor for defining the validation specifics.

In the “Type” combo select “List” and select the attribute on which you want to define the list validator. In my demo I have selected “DepartmentName”. Select the operator (In or NotIn) and the List Type (Literal Values, Query Result, View Object Attribute or View Accessor Attribute). In my demo i have selected the "In" operator and "Literal Values" as the List Type.

In the "Enter List of Values" text box enter the possible values a department can have (enter each values without quotes on a new line).
In the "Failure Handling" tab define a failure message and click "OK". In my example I have used a message token expression to construct a dynamic error message, passing to the failure message the department name.

If you inspect the Department’s entity source code you will see that JDeveloper added a ListValidationBean tag to the XML entity definition file.
Before testing our validator please ensure that your application module has the Departments view selected under the “Data Model”.

To test your validator, run the Application module and try to update a department's name to "Sales" (if you recall i did not include sales in my list of allowed literal values). You should get your custom dynamic error message displayed.

Please note that list validators based on SQL queries or View Object attributes retrieve all rows of a query each time a validation is perform, therefore you should be very careful when you choose to use these list types. It it recommended that you use these list types only for relatively small value sets.

Download sample application: List Validator

No comments:

Post a Comment