The “Key Exists” Validator is validator that can be defined at either the entity level or the attribute level but pertains to an entity object attribute to check whether a key exists based on a primary key, foreign key or an alternate key. The “Key Exists” validator will check first the cache querying rows not committed yet to the database and if the key is not found in the cache will run a check in the database.
So let’s start by creating a new ADF Fusion Web Application and create the basic business components that we will be using in this demo, two entity objects based on the Countries and Regions HR tables, their associated view objects and a default application module.
Next let’s define a “Key Exists” validator on the Countries entity object. So on “Business Rules” tab of the Countries 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 “Key Exists” and you will see that in the “Validation Target Type“ there are three options; to target the validation at the “Entity Object” level meaning that this validation will be used for all view objects that use this entity attribute, “View Object” or “View Accessor”. In my demo I will be targeting my “Key Exists” validator at the entity object.
In the “Association Name“ combo you will get displayed with all the associations pertaining the entity object. In my case I have only one association, the “CountryRegFkAssoc”.
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 country’s name and country’s region id.
Before testing our validator please ensure that your application module has the Countries view and Regions View selected under the “Data Model”.
To test our newly defined "Key Exists Validator" run the Application module and and try to create a country with an invalid region id. You should get your custom dynamic error message displayed.
Now create a new region, do not commit the new region and try to create a new country based on the new region’s id. The “Key Exists” validator will check the cache and will identify that the region id exists (even though it’s not committed in the database).
Download sample application: Key Exists Validator