1

Custom Tabular Form Validations

This is a follow up post to “Combining your item validations into one, without losing inline display”. Whilst most of the techniques I describe (present and past) are not officially supported, the point to note is that they simply exist because APEX currently does not provide the required functionality. The thing I like about APEX is that there is a lot of flexibility to interact with the engine during page rendering and processing, at the same time though we need to ensure that we do this in a safe and future proof manner, or at least be aware of the risks and properly document them.

Custom Tabular Form Validation

So the point of today’s post is to fulfil the requirement of creating complex tabular form validations without losing the benefits of inline display. Now I’m sure this will be coming in the next APEX 4.1 release, but as always I have an immediate requirement. So to fill the void in the meantime I’ve extended the “LOG_VALIDATION_ERROR” function defined in the earlier post to include support for tabular forms ( it’s simply a wrapper for code centralization purposes ).

-- ---------------------------------------------------------------------------
--
-- Procedure      : log_validation_failure
-- Author         : Matt Nolan
-- Private/Public : Public
-- Description    : Wrapper for logging validation failures in the APEX engine
-- ---------------------------------------------------------------------------
-- Revision History
-- Date            Author       Reason for Change
-- ---------------------------------------------------------------------------
-- 02 FEB 2011     M.Nolan      Created.
-- ---------------------------------------------------------------------------
PROCEDURE log_validation_failure
( p_item          IN VARCHAR2
, p_message       IN VARCHAR2
, p_location      IN VARCHAR2 DEFAULT 'INLINE_WITH_FIELD'
, p_column_id     IN VARCHAR2 DEFAULT NULL
, p_column_name   IN VARCHAR2 DEFAULT NULL
, p_row_num       IN VARCHAR2 DEFAULT NULL
)
AS

  l_app_id     VARCHAR2(10) := apex_application.g_flow_id;
  l_page_id    VARCHAR2(10) := apex_application.g_flow_step_id;
  l_item_id    apex_application_page_items.item_id%TYPE;
  l_index      PLS_INTEGER;

BEGIN

  apex_application.add_validation_error
  ( p_message                => p_message
  , p_error_display_location => p_location
  , p_page_item_name         => p_item
  , p_column_id              => p_column_id
  , p_column_name            => p_column_name
  , p_row_num                => p_row_num
  );  

  IF apex_application.g_debug THEN
    apex_debug_message.log_message('Custom Validation');
    apex_debug_message.log_message('...Item: '||p_item);
    apex_debug_message.log_message('...Message: '||p_message);
    apex_debug_message.log_message('...Location: '||p_location);
    IF p_column_id IS NOT NULL THEN
      apex_debug_message.log_message('...Column ID: '||p_column_id);
      apex_debug_message.log_message('...Column Name: '||p_column_name);
      apex_debug_message.log_message('...Row Number: '||p_row_num);
    END IF;
  END IF;

EXCEPTION
  WHEN OTHERS THEN
    -- custom error handler goes here
    RAISE;
END log_validation_failure;

Here’s an example of how we would code a custom tabular form validation ( Note: its a PLSQL page level validation which always returns TRUE as we add the errors ourselves within the function):


FUNCTION mytabform_validation
( p_tabform_pk      IN APEX_APPLICATION_GLOBAL.VC_ARR2
, p_tabform_column  IN APEX_APPLICATION_GLOBAL.VC_ARR2
, p_column_id       IN VARCHAR2
, p_column_name     IN VARCHAR2
, p_message         IN VARCHAR2
) RETURN BOOLEAN
AS

  l_student_id        user_volts_reservation_day_vw.student_id%TYPE;
  l_reservation_id    user_volts_reservation_day_vw.reservation_id%TYPE;
  l_message           VARCHAR2(500);

BEGIN

  FOR i IN 1.. p_tabform_column.count LOOP

    l_pk_id := p_tabform_pk(i);
    l_value := p_tabform_column(i);

    IF NOT some_validation
          ( p_pk               => l_pk_id
          , p_value            => l_value
          , p_message_override => l_message
          )
    THEN
      log_validation_failure
      ( p_item        => NULL
      , p_message     => nvl(l_message,p_message)
      , p_location    => 'INLINE_WITH_FIELD_AND_NOTIFICATION'
      , p_column_id   => p_column_id||'_'||lpad(i,4,'0')
      , p_column_name => p_column_name
      , p_row_num     => i
      );
    END IF;
  END LOOP;
  --
  -- Always return true as we validate and add the error messages ourselves
  --
  RETURN TRUE;

EXCEPTION
  WHEN OTHERS THEN
    -- your custom error handler goes here
    RAISE;
END;

And here is how we’d call the validation, by passing in the column arrays which we are interested in validating along with the column_id of the field we are validating and its corresponding name. In this example I also pass through the tabular form primary key as well to use in table lookups during validation.

RETURN mytabform_validation
      ( p_tabform_pk     => apex_application.g_f02
      , p_tabform_column => apex_application.g_f03
      , p_column_id      => 'f03'
      , p_column_name    => 'STUDENT_ID'
      , p_message        => 'Student is booked on another course!'
      );

Note: the “column_id” and “g_f0x” array items can be determined by using firebug and inspecting the input elements in the tabular form on the page. Alternatively you could omit them and do a lookup of the APEX Data Dictionary within the validation based on the column name passed through to identify the required id’s.

If you need extra validation control on your tabular form but are uncomfortable with using the undocumented “ADD_VALIDATION_ERROR” procedure, then have a look at an alternative post Denes Kubicek wrote late last year. Otherwise I hope you find this information useful, as it’s helped me on my current project immensely.

Finally if one of the APEX development team members happens to read this ( not naming and shaming names :) ) a little inside knowledge of whether this will break in the next 4.1 release would be invaluable.

  1. Patrick Wolf says:

    Hi Matt,

    you should be fine with 4.1. I can’t 100% guarantee that the apex_application.add_validation_error API stays the same (p_column_id might get removed) but you will definitely still have some APIs to add validation errors to our error stack. BTW, in 4.1 it should be a lot easier to create tabular form validations for all validation types (including Exists, PL/SQL, …). So no need anymore to know the apex_application.g_fxx mapping.

    Regards
    Patrick