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.

  1. Patrick Wolf says:

    Hi Matt,

    as you have already written in your article, “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.” I can you already tell that this will break in 4.1 and I do not recommend to use unsupported internal arrays of APEX.

    In exchange I can offer you to have a look at the procedure apex_application.add_validation_error. Sure it’s also not officially documented, but it’s not planned to change it the near future and it does the same as your own code, but with the big advantage that you don’t having to directly deal with the internal arrays.

    Regards
    Patrick

  2. Hi Matt,

    Pretty neat idea!

    Martin