This discussion is archived
5 Replies Latest reply: Feb 14, 2012 11:08 AM by 492910 RSS

validate if user entered at least one page item

492910 Newbie
Currently Being Moderated
Apex Version: Application Express 4.1.0.00.32
Web browser: Internet Explorer 8
Oracle DB: 11g Enterprise Edition Release 11.2.0.1.0

Is there a way to validate if a user has entered at least one item on a form?

I have a function:
function hasPageValues return boolean
is
v_item_found boolean := false;
begin

for rITM in
( select item_name
from apex_application_page_items
where application_id = Apex_Application.g_flow_id
and page_id = Apex_Application.g_flow_step_id
)
loop
-- does the item have a value
if v(rITM.item_name) is not null then
v_item_found := true;
end if;
end loop;

return v_item_found;
end hasPageValues;

I call this function returning boolean under my page validation. It doesn't seem to be working. It always returns false. What am I missing and is there a better way of doing this type of validation. Sorry I am new to APEX. Thank you so much!
  • 1. Re: validate if user entered at least one page item
    fac586 Guru
    Currently Being Moderated
    Function logic is incorrect. It loops through all items irrespective of whether it's already found one that is not null, so a later one that's null will overwrite any previous true result. The loop needs to exit when it hits a true result:
    ...
    if v(rITM.item_name) is not null then
      v_item_found := true; 
      exit;
    end if;
    ...
    But I wouldn't do it that way.
    where application_id = Apex_Application.g_flow_id
    and page_id = Apex_Application.g_flow_step_id
    Use documented APIs and substitution strings where these exist: <tt>:APP_ID</tt> and <tt>:APP_PAGE_ID</tt> in this case. (If something's not documented then re-evaluate whether you should be using it at all...)

    Back up and explain the requirement behind this in more detail. Including:

    <li>Do you intend to reuse this function on a number of pages?
    <li>How many items are likely to be involved per page?
    <li>Do you really need to be checking all of the items on the page? Or only those in a certain region or regions?
    <li>What types of items are involved?
    <li>Will there be any hidden items on the page?

    Edited by: fac586 on 26-Jan-2012 08:25
  • 2. Re: validate if user entered at least one page item
    492910 Newbie
    Currently Being Moderated
    Do you intend to reuse this function on a number of pages? Yes

    How many items are likely to be involved per page? around 80 items

    Do you really need to be checking all of the items on the page? Or only those in a certain region or regions? all items in one region

    What types of items are involved? checkboxes, text boxes, and date feilds

    Will there be any hidden items on the page? yes hidden text boxes

    Thanks!!!
  • 3. Re: validate if user entered at least one page item
    492910 Newbie
    Currently Being Moderated
    Also I added the exit part in the code above and it still does not work. Still always returns false.

    Edited by: SmV on Jan 26, 2012 8:41 AM

    Edited by: SmV on Jan 26, 2012 8:42 AM
  • 4. Re: validate if user entered at least one page item
    fac586 Guru
    Currently Being Moderated
    SmV wrote:
    Also I added the exit part in the code above and it still does not work. Still always returns false.
    When you have a problem in APEX you can use debug mode to trace what the application is doing. It's good practice to include instrumentation code in your program units that will generate useful debug information. Use the apex_debug_message API to do this:
    create or replace function hasPageValues (
        p_app_id  in pls_integer
      , p_page_id in pls_integer)
      return boolean
    is
      v_item_found boolean := false;
    begin
    
      for rITM in (
        select
                  item_name
                , display_as
        from
                  apex_application_page_items
        where
                  application_id = p_app_id
        and       page_id = p_page_id)
      loop
        apex_debug_message.log_message(rITM.item_name || ' (' || rITM.display_as || '): ' || v(rITM.item_name));
        -- does the item have a value
        if v(rITM.item_name) is not null
        then
          apex_debug_message.log_message(rITM.item_name || ' is not null: exit.');
          v_item_found := true; 
          exit;
        end if;
      end loop;
    
      apex_debug_message.log_message('hasPageValues = ' || case when v_item_found then 'TRUE' when not(v_item_found) then 'FALSE' else null end);
    
      return v_item_found;
    
    end hasPageValues;‚Äč
    With no data entered in the page, this yields the following debug trace:
    ...
    0.19096     0.00084     Perform custom validations:
    0.19180     0.00081     ...Validation "hasPageValues" - Type: FUNC_BODY_RETURNING_BOOLEAN
    0.19261     0.09948     ...Execute Statement: declare function x return boolean is begin return hasPageValues(:app_id, :app_page_id); return null; end; begin wwv_flow.g_boolean := x; end;
    0.29213     0.00144     P6_PRODUCT_IMAGE (File Browse...):
    0.29352     0.00082     P6_IMAGE (Display Only):
    0.29434     0.00075     P6_PRODUCT_ID (Hidden):
    0.29509     0.00105     P6_PRODUCT_NAME (Text Field):
    0.29614     0.00083     P6_PRODUCT_DESCRIPTION (Textarea):
    0.29697     0.00104     P6_CATEGORY (Radio Group):
    0.29801     0.00151     P6_PRODUCT_AVAIL (Radio Group):
    0.29952     0.00079     P6_LIST_PRICE (Number Field):
    0.30030     0.00071     hasPageValues = FALSE
    0.30102     0.00066     ......Did NOT pass
    0.30168     0.00080     Add error onto error stack
    0.30249     0.00079     ...Error data:
    0.30327     0.00299     ......message: Page has no values
    whilst with an item value set, we get:
    0.07573     0.00107     Perform custom validations:
    0.07680     0.00094     ...Validation "hasPageValues" - Type: FUNC_BODY_RETURNING_BOOLEAN
    0.07774     0.00231     ...Execute Statement: declare function x return boolean is begin return hasPageValues(:app_id, :app_page_id); return null; end; begin wwv_flow.g_boolean := x; end;
    0.08005     0.00105     P6_PRODUCT_IMAGE (File Browse...):
    0.08109     0.00077     P6_IMAGE (Display Only):
    0.08187     0.00094     P6_PRODUCT_ID (Hidden):
    0.08281     0.00074     P6_PRODUCT_NAME (Text Field):
    0.08354     0.00087     P6_PRODUCT_DESCRIPTION (Textarea):
    0.08442     0.00072     P6_CATEGORY (Radio Group):
    0.08514     0.00067     P6_PRODUCT_AVAIL (Radio Group): Y
    0.08581     0.00112     P6_PRODUCT_AVAIL is not null: exit.
    0.08693     0.00076     hasPageValues = TRUE
    Both of which appear to exhibit the expected behaviour. What happens when you do this?
  • 5. Re: validate if user entered at least one page item
    492910 Newbie
    Currently Being Moderated
    Thank you so much! I didn't realize I had a hidden feild with a default value. I had to exclude that feild. Thanks for your help!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points