Checking if a Page Item has been rendered or not

Did you ever have had the requirement that a validation should only fire if the page item has been rendered (eg. if you have defined a condition or authorization)?

As long as you set the “Associated Item” property in the validation to the page item you want to check, you are fine. Oracle Application Express (APEX) will do the check for you and only fire the validation if it’s there. BTW, I didn’t know that – really nice! :-) But if you create a page level validation it’s up to you to determine if the item is there or not.

So you would probably go and duplicate your condition/authorization code to the page level validation. Not good! Twice the code to maintain.

You don’t have to!

There is a nice tip posted on the OTN forum by Scott Spadafo which uses the Apex_application.g_arg_names array to determine if the item is there or not.

Attached you will find a function which you can use in a PL/SQL based condition (eg. PL/SQL expression). Call it with your page item name and it will return TRUE if the item has been rendered.

CREATE OR REPLACE FUNCTION isPageItemRendered
  ( pName IN VARCHAR2
  )
  RETURN BOOLEAN
IS
    vPageItemId NUMBER;
BEGIN
    ----------------------------------------------------------------------------
    -- Get the internal id of the page item.
    ----------------------------------------------------------------------------
    BEGIN
        SELECT ITEM_ID
          INTO vPageItemId
          FROM APEX_APPLICATION_PAGE_ITEMS
         WHERE APPLICATION_ID = WWV_Flow.g_flow_id
           AND PAGE_ID        = WWV_Flow.g_flow_step_id
           AND ITEM_NAME      = UPPER(pName)
        ;
    EXCEPTION WHEN NO_DATA_FOUND THEN
        RAISE_APPLICATION_ERROR(-20001, 'Page item '||pName||' doesn''t exist!');
    END;
    ----------------------------------------------------------------------------
    -- If a page item is rendered the internal id is stored in a hidden field
    -- called p_arg_names. During submit the values are stored into the
    -- g_arg_names array by the WWV_Flow.accept procedure.
    -- By checking for existence of the page item id in the array, we are able
    -- to determine if APEX has rendered the item as "Saves state".
    -- Note: A item which is normally enterable but which is rendered
    --       "Read Only" is also considered rendered, because it still saves
    --       state.
    ----------------------------------------------------------------------------
    FOR ii IN 1 .. Apex_Application.g_arg_names.COUNT
    LOOP
        IF Apex_Application.g_arg_names(ii) = vPageItemId
        THEN
            RETURN TRUE;
        END IF;
    END LOOP;
    --
    RETURN FALSE;
    --
end isPageItemRendered;