The my last two posts I’ve demoed how to use the “CollectionValidator” and the “Unique Key Validator”, two declarative validation options
that can be defined at the entity level.
In the following six posts I will be going through the
declarative validation rules that can be defined either at the entity or
attribute level but pertain to an entity object attribute and these are the
“Compare Validator”, the “Key Exists Validator”, the “Length Validator”, the
“List Validator”, the “Range Validator” and the “Regular Expression Validator”.
So the “Compare Validator” is a validator that can be
defined either at the entity or the attribute level but relates to an entity
object attribute and it used to perform a logical comparison between an entity
attribute and another value. This other value could be a literal value, the
result of a query, a view object attribute, a view accessor attribute, an
expression or an entity attribute. I will demo all these options.
So let’s start by creating an application, in my case it is called "CompareValidatorDemo" and create the basic ADF BC objects for this tutorial. For creating the ADF BC objects (entities and views) i have used the "Business Components from Tables" wizard and created entities for the Employees, Departments, Regions and Countries table, their corresponding view objects and an application module as depicted in the screenshot below.
Let's define the first "Compare Validator" rule on the Employees entity to ensure that an employee's salary does not exceed a defined amount. On the Employees entity, under the "Business Rules" tab click the green plus button "Create new validator".
From the "Add Validation Rule" window select "Compare" in the "Type" drop down and from the "Rule Definition" tab select "Salary" in the "Attribute" drop down, "LessOrEqualTo" as the "Operator" and in the "Compare With" combo select "Literal Value". Enter a value in the "Enter Literal Value" field.
In the "Failure Handling" tab define a failure message and click "OK". In my example I have used two message token expressions to construct a dynamic error message, passing to the failure message the employee's first name and last name.
Let's see now how you can compare an Entity attribute against the results of a query. I will define a new "Compare Validator" but this time i will enforce a business rule to make sure a user can't specify a future date as the employee's hire date.
So on the Employees entity, under the "Business Rules" tab click the green plus button "Create new validator" and create a new "Compare Validator" business rule. From the "Add Validation Rule" window select "Compare" in the "Type"
drop down and from the "Rule Definition" tab select "HireDate" in the
"Attribute" drop down, "LessOrEqualTo" as the "Operator" and in the
"Compare With" combo select "Query Result" and enter "select sysdate from dual" as an SQL statement.
In the "Failure Handling" tab define a failure message and test the new business rule you've defined by running the Application
module and updating one of the employee's hire date to a past date.You should get your error message displayed.
You can implement the exact same business rule using a Groovy expression. So instead of using the "Query Result" option in the "Compare With" combo, select "Expression" and enter "adf.currentDate" as a Groovy expression to get the current date.
Run the Application
module and update one of the employee's hire date to a past date.You should get your error message displayed.
You can also compare an Entity's attribute against a view object's attribute using the "View Object Attribute" option in the "Compare With" drop down. This should be used with great caution as it will use the first row's attribute for comparison.
Just for demo purposes i will be comparing the entity's first name against the Employee's view object last name to ensure they are not the same.
Define an error message in the "Failure Handling" tab and run the application module to test this business rule. The first record that you get when accessing the Employees view is Steven King and the second employee is Neena Kochhar. Go to another employee, for example to employee with id 102 (Lex De Haan). Update Lex's last name to Kochhar and click the "Validate Row" button. The employee's last name is valid. Now update Lex's last name to King and validate the record. You should get your error message displayed.
So a "View Object Attribute" will compare an entity's attribute against the first row's attribute. If you want to compare an entity's attribute against the entire set of view object attribute values you would use the "View Accessor Attribute" option which requires a "View Accessor" to be created.
For this demo i will be using the "Countries" entity to ensure a country's name is not the same as the region it belongs to. To do so i will have to first create a view criteria on the "Regions" view object as follows.
Then, on the "Countries" entity, under the "View Accessors" tab, create a new accessor by shuffling the "RegionsVO" view object from the "Available View Objects" list to the "View Accessors" list.
Having the "RegionsVO" selected click on the "Edit" button and shuffled the view criteria you've created on the "Regions" view from the available list to the selected list.If you've followed my exact steps you should have a parameter (regionId) that you should bind to the entity's RegionId parameter.
Create a new "Compare" validator and use the newly created accessor to compare the entity's country name to it's corresponding region name.
Define an error message and run the application module to test your business rule. The first country once you access the Countries view is Argentina and has "Americas" defined as it's region name. Update Argentina's country name to Europe and validate the record. Validation is successful. Update Argentina's country name to Americas and validate the record. Validation should fail and you should get your custom error message.
The last feature that i would like to demo on the "Compare Validator" is the ability to compare an entity's attribute against another attribute from the same entity. For example, ensure that an employee can't have themselves defined as their manager.
For this example i will be using the "Entity Attribute" option from the "Compare With" drop down to define this business rule.
Define an error message and run the application module to test your business rule. Update Steven King's manager to Neena Kochhar (with employee id = 101) and validate the employee record. Validation is successful. Define Steven King as Steven King's manager and validate the record. The validation will fail and you will get your custom error message displayed.
Download sample application: Compare Validator