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.

2

PL/SQL function returning a SQL Query


Do region plugins support defining the plugin SQL source as a “PL/SQL function returning a SQL Query”? You might be wondering why I’m asking the question. The reason is: you can define a function body returning a string and not receive a parsing error, however if you try and execute it using the following you will get an error.

l_sql_handler := apex_plugin_util.get_sql_handler
                ( p_sql_statement  => l_source_sql
                , p_min_columns    => 2
                , p_max_columns    => 100
                , p_component_name => NULL
                );
--
-- Now prepare the query. This is where all the binding is done.
--
apex_plugin_util.prepare_query
( p_sql_handler => l_sql_handler
) ;
--
-- We're use get_data2 to ensure the data comes back in their natural data types
--
l_row_set := apex_plugin_util.get_data2
             ( p_sql_handler => l_sql_handler
             ) ;

The package code apex_plugin_util does not recognize that your region source is a PLSQL function and treats it as a normal query. I’m pretty sure this has occurred because the plugin wizard reuses the same SQL validation routine used for the creation of report regions. I’m guesstimating purely because report regions allow you to define queries via a “PL/SQL function returning a SQL Query”.

Now am I complaining about this? No way, I’m over the moon about this possible oversight/unimplemented feature. This is because I’m going to take full advantage as sometimes I need to dynamically build my SQL queries for complex solutions. So in order to support this in my plugin code all I need to do is execute the function before I call the plugin API handlers to execute my returned query string.

Which brings me to the actual reason for writing this post (I’m hoping to get some feedback on this). How does one actually use EXECUTE IMMEDATE to call an anonymous function since only anonymous procedures are supported? I did a bit of Googling and found a few interesting posts but nothing concrete that would guide me with an exact example. Since I’m paid by the hour, it’s hard to justify spending half a day trying to find the best solution, so I just had to come up with something on the spot. So here’s my attempt (which does work) but I’m more than happy for anyone to point out a better solution. UPDATE: Patrick Wolf has enlightened me with a much simpler method, read the comments on this post.

Note: the following assumes you define your “PL/SQL function returning a SQL Query” within a BEGIN and END block.

--
-- Replace binds with V('ITEM') thanks to Andy Tulley
--
l_source_sql := regexp_replace(l_source_sql,':([a-zA-Z0-9_]*)','v(''\1'')');
--
-- We'll remove the DECLARE reserved word if it's defined and
-- create an inline function within an anonymous procedure which
-- we can then call
--
EXECUTE IMMEDIATE 'DECLARE FUNCTION myfunc RETURN VARCHAR2 AS '||
                  regexp_replace(l_source_sql,'DECLARE','',1,0,'i')||
                  ' BEGIN :b1 := myfunc; END;'
USING OUT l_source_sql;

Here’s an improved version of the above (but more complicated and harder to read) which uses binds rather than replacing them with v(‘ITEM’) in the query. This is for performance reasons and in case you have a colon in your query which is not a bind variable e.g. APEX URL. You can use either or…

IF regexp_instr(l_source_sql,'(declare)?.*begin.*end;',1,1,1,'in') > 0 THEN

  ----------------------------------------------------------------------------
  -- Get bind variables.
  ----------------------------------------------------------------------------
  l_binds :=  wwv_flow_utilities.get_using_clause(l_source_sql);  

  IF l_binds IS NOT NULL THEN
    EXECUTE IMMEDIATE
        'DECLARE l_sql VARCHAR2(32767); BEGIN' ||
        '    EXECUTE IMMEDIATE ''DECLARE FUNCTION myfunc RETURN VARCHAR2 AS ' ||
          REPLACE
          ( regexp_replace
            ( l_source_sql
            , '('||CHR(13)||'|'||CHR(10)||'|DECLARE)'
            ,' ',1,0,'in'
            )
          , ''''
          , ''''''
          ) ||' BEGIN :b1 := myfunc; END;'''||
        '  ' || l_binds || ', OUT l_sql;'||
        ' :b1 := l_sql; END;'
    USING OUT l_source_sql;
  ELSE
     EXECUTE IMMEDIATE 'DECLARE FUNCTION myfunc RETURN VARCHAR2 AS '||
                       regexp_replace(l_source_sql,'DECLARE','',1,0,'i')||
                       ' BEGIN :b1 := myfunc; END;'
     USING OUT l_source_sql;
  END IF;

END IF;

Now I am hoping that on the odd chance the APEX development team stumble across this blog they don’t remove this behaviour in a future release. On the other hand I’d be more than happy if they support this feature within the native plugin API which will mean one less step that we need to do. The bright side is that there is some extra flexibility in the plugin design, intentional or not.

3

Combining item validations into one, without losing inline display

Page item validations, whilst very useful under normal circumstances can become a management nightmare if you have a large number of items defined on the page that require several validations per item. This is especially evident when you use repeating fields on the same page as per the below form design which I’m currently working on for a customer (in APEX 4.0).

Add to this: catering for conditional display of the items and conditional firing of the validations you’ve got yourself a big headache on your hands. Now I’m sure some of you are shaking your heads going, “No you can use a page level validation a write a single validation” instead of defining 3 or 4 validations per item for all items. The trade off though is not being able to associate your page level validation with an item. Now when you have 40+ items on a page and no highlighting of the actual item which raised the error, rest assured your end user is going to explode in a rage after submitting the page for the tenth time still wondering what the problem field is.

Now I’m not the first to complain about validation support, to be honest in their current form they are still quite usable, but as in life there is always room for improvement. Many have previously requested validation groups, dependencies etc. etc. I’d second those and perhaps also suggest an array page item, similar to how a tabular form field operates, we would then only need one item definition and one validation for a repeating field such as a number of dates as in the above screenshot.

Now for those of you, like myself, who can’t wait for enhancements… we can work around this issue with a little PLSQL and using an internal undocumented procedure ADD_VALIDATION_ERROR in APEX_APPLICATION (synonym translated to WWV_FLOW).

-- ---------------------------------------------------------------------------
--
-- Procedure      : log_validation_failure
-- Author         : Matt Nolan
-- Private/Public : Public
-- Description    : Wrapper for logging validation failure in 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'
)
AS

BEGIN

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

  IF apex_application.g_debug THEN
    apex_debug_message.log_message('AF Custom Item 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);
  END IF;

EXCEPTION
  WHEN OTHERS THEN
    --logger.log_error('log_validation_failure','Item: '||p_item||' '||SQLERRM);
    RAISE;
END log_validation_failure;

What the above will do is add a validation error the “stack”, so it can be combined with existing standard item validations. Also when calling the above procedure I normally have my items in an array of records which I loop through and perform the custom validations which I have designed. Here’s an idea of the concept e.g.

FOR i IN 1.. page_items.count LOOP
  IF i < page_items.count THEN
    IF NOT validate_dates(v(page_items(i).item_name),v(page_items(i+1).item_name)) THEN
      log_validation_failure
      ( p_item      => l_item_name
      , p_message   => 'must be < Day '||to_number(page_items(i).seq+1)
      );
    END IF;
  END IF;

  IF NOT is_asset_available
        ( p_course_id         => p_course_id
        , p_date              => p_date
        , p_start             => p_date
        , p_finish            => p_date
        , p_no_students       => p_no_students
        , p_ins_server        => p_ins_server
        , p_no_students_srvr  => p_no_students_srvr
        , p_virt_classroom    => p_virtual_classroom
        , p_out_message       => l_msg
        )
  THEN
    log_validation_failure
    ( p_item      => l_item_name
    , p_message   => l_msg
    );
  END IF;
END LOOP;

I then create a single validation on the page with the type “Function returning Boolean” that has my custom validation function call and always returns “TRUE”. It is a very key point that our actual validation never fails as we actually populate the errors ourselves within the function call. The validation we define on the APEX page is merely a wrapper around the group of real validations which we’ve created in our custom PLSQL.

In addition to the above you may also want to check out an older post I wrote about updating the REQUEST value within your custom PLSQL. This can give you more control as to what validations actually fire when using the built-in condition in combination with the above technique and standard validations.

The end result is a single group validation with inline item error display which translates into reduced maintenance, more control, and de-cluttering of the page in the IDE (well in the old view). The point to note is that using this method may mean that the validations you define may potentially break when upgrading to future releases of APEX. That is if the development team change the current validation design. For me this is something I can live with as the benefits outweigh the risks. However it is something for you to note if you intend to follow this post. Note: if you read the comment posted by Patrick Wolf, you will see that using this method will be fine at least for the 4.1 release. The original code I posted populated the validation arrays directly, however I have ammended it to use the procedure “add_validation_error” after Patricks suggestion.

On the topic of validations I must thank Guido Zaleen for the Form Validator plugin which he posted on APEX-PLUGIN.com. This works extremely well and is very thorough in design. I love the fact that it even takes jQuery UI tabs on the page into account and will automatically tab-switch if the error is on a hidden tab at page render. The code is also easy enough to follow and add additional custom validations as well. If you’re looking for client side and AJAX validations don’t write your own, this plugin is perfect. Great work Guido.

Pages ... 1 2