The Business Objects layer in Oracle Visual Builder exposes database tables through REST APIs that you can access from your UI and other places. In addition, you can define business logic in this layer. Defining business logic on the BOs can ensure that data that goes into the tables through the REST APIs is valid. In this blog we focus on the validators you can define on business objects.
In the Business Objects' business logic tab you'll find two types of validators – a field validator and an object validator. You'll use the field validator to check the new value of a specific field. You'll use the object validator to check the a combination of values in several fields across the row. Note that the field validator has access to the new value of the specific field – but for the other fields it will be looking at the values before any changes – this is why you need a row validator.
The validator needs to return a boolean value – true if the value is valid, and false for an invalid value. When you define a validator you also provide an error message that will be returned to the client who made the change if the validation failed.
Beyond directly referencing the values of fields in the BO you can also use groovy functions as part of the validator. You can see a list of the functions on the left side of the code editor – clicking on a function will add it to the code editor. The functions are displayed based on the type of field they work on.
Let's look at a couple of examples of validators and their syntax.
We'll use a sample business object called employees with the fields: Name -text field, Manager – boolean field, Bonus and Salary – numeric fields.
Here is how you'll define the following rules:
New salary must be bigger than the previous salary – field validation on the salary field:
newValue > oldValue
Don't allow name to be James or John (regardless of upper/lowercase letters) – field validation:
lowerCase(newValue)!='james' && lowerCase(newValue)!='john'
Bonus can't be empty, but must be less than half the salary – object validator:
bonus && bonus < salary/2
If the name is Shay (ignore upper/lowercase) the salary needs to be more than 2000 for non-manager and over 4000 for managers – object validator:
if (lowerCase(name)=='shay') {
if (manager) {
salary > 4000
} else {
salary > 2000
}
} else {
true;
}
While developing the rules it's useful to remember that you can disable a validation rule without completely removing it. If you run into errors the log tab for the business objects can be helpful to see errors the BO layer raises.
As you can see you can create quite complex rules to validate your data. To learn more about the syntax and option check our Groovy reference book that has a section about validators.
