Search This Blog

Showing posts with label Associate a Validation with a Specific Item. Show all posts
Showing posts with label Associate a Validation with a Specific Item. Show all posts

Tuesday, June 24, 2025

How Do I Associate a Validation with a Specific Item

 Associating a Validation with a Specific Item in Oracle APEX

Validations ensure that user inputs meet specific requirements before data is processed or saved. In Oracle APEX, you can associate a validation with a specific page item to ensure that the validation applies only to that field.

This tutorial explains how to create and associate different types of validations with a specific item in APEX, along with best practices for improving the user experience.


Understanding Item-Specific Validations

When creating a validation in APEX, you can associate it with a specific item so that it only triggers when that item is interacted with.

Validations can be applied to:

  • Text fields (e.g., ensuring a name is not empty)

  • Number fields (e.g., checking if a value is within a range)

  • Select lists (e.g., ensuring a selection is made)

  • Date pickers (e.g., checking if a date is in the future)

Each validation can be triggered on form submission, ensuring incorrect data is not processed.


Creating a Validation for a Specific Item

Step 1: Open Page Designer

  1. Navigate to App Builder and open your application.

  2. Select the page where the validation should be applied.

  3. In Page Designer, locate the item you want to validate (e.g., P1_EMAIL).


Step 2: Creating a Required Field Validation

To ensure a field is not left empty, create a Not Null validation.

  1. Select the P1_EMAIL item.

  2. Scroll down to the Validation section and click Create.

  3. Choose Validation Type as Not Null.

  4. Set the Associated Item to P1_EMAIL.

  5. Enter an Error Message, such as:

"Email address is required. Please enter a valid email."

  1. Click OK, then Save and Run the Page.

  2. Test by submitting the form without entering an email. The error message should appear.


Step 3: Validating Email Format with a Regular Expression

To ensure a user enters a valid email address, use a Regular Expression validation.

  1. Select the P1_EMAIL item in Page Designer.

  2. Go to the Validation section and click Create.

  3. Choose Validation Type as Regular Expression.

  4. Set the Expression to:

^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$

  1. Set the Associated Item to P1_EMAIL.

  2. Enter an Error Message, such as:

"Invalid email format. Please enter a valid email address."

  1. Click OK, then Save and Run the Page.

  2. Test by entering an invalid email to see the error message.


Step 4: Checking If a Value Exists in the Database

To prevent duplicate entries, use a PL/SQL Function Returning Boolean validation.

  1. Select the P1_USERNAME item in Page Designer.

  2. Go to the Validation section and click Create.

  3. Choose Validation Type as PL/SQL Function Returning Boolean.

  4. Enter the following PL/SQL code:

DECLARE

    v_count NUMBER;

BEGIN

    SELECT COUNT(*) INTO v_count 

    FROM USERS 

    WHERE USERNAME = :P1_USERNAME;


    RETURN v_count = 0;

END;

  1. Set the Associated Item to P1_USERNAME.

  2. Enter an Error Message, such as:

"This username is already taken. Please choose another."

  1. Click OK, then Save and Run the Page.

  2. Test by entering a duplicate username to trigger the validation.


Step 5: Validating a Number Field

To ensure a user enters a number within a certain range, use a SQL Expression validation.

  1. Select the P1_AGE item in Page Designer.

  2. Go to the Validation section and click Create.

  3. Choose Validation Type as SQL Expression.

  4. Enter the condition:

:P1_AGE BETWEEN 18 AND 65

  1. Set the Associated Item to P1_AGE.

  2. Enter an Error Message, such as:

"Age must be between 18 and 65."

  1. Click OK, then Save and Run the Page.

  2. Test by entering an invalid age to trigger the validation.


Best Practices for Item-Specific Validations

  • Always associate validations with a specific item to provide clear error messages.

  • Use Not Null validations for required fields.

  • Use Regular Expressions for format validations (e.g., email, phone numbers).

  • Use PL/SQL for database-related validations (e.g., checking for duplicates).

  • Use SQL Expressions for range-based validations.

  • Always provide friendly error messages to guide the user.


Associating validations with specific items in APEX ensures that data is accurate before submission. By using Not Null, Regular Expressions, PL/SQL, and SQL Expressions, you can enforce rules and provide meaningful feedback to users. This helps prevent errors and improves the overall user experience.


EXAMPLE NOTES

To associate an item with a validation and specify error message text:

  1. View the page in Page Designer:

    1. On the Workspace home page, click the App Builder icon.

    2. Select an application.

    3. Select a page.

  2. Page Designer appears.

  3. In Page Designer, select the Processing tab in the left pane.

  4. Under Validating, select the validation you want to associate.The Property Editor displays Validation attributes.

  5. Under Validation, edit the following attributes:Error, Error Message - Enter the text to be displayed in the event that the validation does not pass.

  6. Error, Display Location - Select where the error message displays for this validation. Validation error messages display on a separate error page, or inline with the existing page. Inline error messages display underneath the associated item label or in a notification area defined as part of the page template.