Skip to Main Content

Conditional Validations

Source: http://www.talkapex.com/2010/06/conditional-validations-in-apex

There may be some instances when you have validations on your APEX page that you only want to run if all the other validations have passed. A good example of this is if you have 2 fields, FROM_DATE and TO_DATE and have the following validations:
- V1: Ensure FROM_DATE is not null and is a valid date
- V2: Ensure TO_DATE is not null and is a valid date
- V3: Ensure FROM_DATE < TO_DATE

In this example you only want the last validation to run if the first 2 pass (i.e. both FROM_DATE and TO_DATE are valid dates). Currently there's no declarative way of doing this. i.e. validations don't support declarative dependencies.

A simple trick to get around this issue is to put a condition in some validations to only run if all the other validations pass. This may not work in all situations, however it should help you in some.

In the example about, modify the 3rd validation (V3) and set the condition to the following:
Condition Type: PL/SQL Expression
Expression 1: apex_application.g_inline_validation_error_cnt = 0

Now V3 will only run if all previous validations pass.

Validations for Dynamic Actions

Validations are usually executed during a page submit, and nice error messages are shown to the user.
However, if you want to validate and process data using a Dynamic Action, you need some JavaScript.

In the example below I have implemented a "Value Required" validation, with some code to get the error message from Apex and the item label.
Try typing both nothing and some text in the item below and hitting the button.

Some useful links:

Code

Item

Identification
Sequence
Name
Display As
User Interface
Region

Button

Name
Button Name
Text Label / Alt
Displayed
Sequence
Display in Region
Behavior
Action
Execute Validations

Dynamic Action

Identification
Name
Execution Options
Sequence
When
Event
Selection Type
Button
Advanced
Event Scope
True Action
Identification
Action
Settings
Code
Execution Options
Sequence
Fire On Initialization
True Action
Identification
Action
Settings
Text
Execution Options
Sequence
Fire On Initialization