In modern business applications, role-based input restrictions are common. For example:
- Certain users can select only a limited number of options in multi-select fields.
- Others have no restriction.
- Sometimes, selection depends on another field value, like a designation.
Implementing such dynamic validations in Oracle APEX can be done efficiently using JavaScript functions combined with Dynamic Actions, keeping logic reusable and maintainable.
Key Components
- Global Validation Function: A single JavaScript function that validates any multi-select LOV based on a controlling field (e.g., designation).
- Dynamic Action on LOV Items: Executes the validation function whenever a user changes selection.
- Safety Dynamic Action on Controlling Field: Clears dependent LOVs if the controlling value changes to prevent invalid data.
Benefits:
- Centralized and reusable logic
- Real-time client-side validation
- Maintains UI integrity
- Minimal code duplication
Use Case Example
We have a Designation field (P22_DESIGNATION) and multiple multi-select LOVs. Rules:
| Designation | Maximum LOV Selections |
|---|---|
| Trainee | 1 |
| Consultant | 2 |
| Principle Consultant / Senior | No restriction |
| Null / Not Selected | Cannot select |
Step 1: Global JavaScript Function
Place this in Global Page → Function and Global Variable Declaration:
function validateSelection(itemName) {
var designation = $v('P22_DESIGNATION');
var val = apex.item(itemName).getValue();
apex.message.clearErrors();
if (!designation) {
apex.item(itemName).setValue('');
apex.message.showErrors([{
type: 'error',
location: ['inline'],
pageItem: itemName,
message: 'Please select Designation first'
}]);
return;
}
if (!val) return;
var values = Array.isArray(val) ? val : val.split(':');
var maxAllowed;
if (designation === 'Trainee') {
maxAllowed = 1;
}
else if (designation === 'Consultant') {
maxAllowed = 2;
}
else if (designation === 'Principle Consultant' ||
designation === 'Principle Senior Consultant') {
return;
}
if (maxAllowed && values.length > maxAllowed) {
var newValue = values.slice(0, maxAllowed).join(':');
apex.item(itemName).setValue(newValue);
apex.message.showErrors([{
type: 'error',
location: ['inline'],
pageItem: itemName,
message: 'Maximum ' + maxAllowed +
' selection(s) allowed for ' + designation
}]);
}
}
Step 2: Apply Dynamic Action on LOV Items
- Event: Change
- Action: Execute JavaScript Code
- Code:
validateSelection(this.triggeringElement.id); - Client-Side Condition: Item is NOT NULL
This ensures the validation runs automatically whenever a user selects values.
Step 3: Safety Mechanism on Designation Change
To prevent inconsistent data:
- Create a Dynamic Action on
P22_DESIGNATION - Event: Change
- Action: Clear all dependent LOVs
Comments
Post a Comment