In Oracle ADF you are not constrained only by built in validation rules, its possible to add new ones. This allows to build custom generic validation rules and use them across project through standard JDeveloper wizard. Today I will explain how you can define such rules, and will use example where PL/SQL function will be invoked to check validation logic.
Download sample application - CustomGenericValidator.zip. This sample implements custom validation rule, where it invokes PL/SQL validation logic for Number type attribute. In PL/SQL function value is checked to be positive, if not - FALSE is returned and validation rule fails.
To define custom validation rule - open Project Properties for Model project and navigate to Registered Rules section under Business Components group. Here you will be able to define new validation rules for your project:
New rule is created in Java class, this class must be specified together with package name and rule display name:
Once new rule is defined in Model project properties, this rule will become available through JDeveloper Validation Rule wizard - custom SalaryValidation rule is defined for Salary attribute in Employees entity:
When custom rule is selected, it brings Rule Definition tab - nothing important here:
On the next tab - Validation Execution, as with built in validation rule, you can specify conditional execution expression:
In Failure Handling tab, failure message is defined to inform user about validation failure:
Now let's look into validator code - validate() method is invoked automatically by the framework. Using input parameter type - JboValidatorContext, I can access current transaction and call PL/SQL function. Based on the result, if it is negative - ValidationException is thrown and user is informed about failure:
PL/SQL function I'm using in this example is really simple - it accepts NUMBER type parameter and if its value is negative, it returns FALSE:
On runtime, validation is successful for positive salary value:
It brings failure message for negative salary value, based on result from PL/SQL function:
The same rule can be reused in other EO's, this means PL/SQL will be always invoked from one central place - custom validator class.