31.7 apex.message namespace

The apex.message namespace is used to handle client-side display and management of messages in Oracle Application Express.

31.7.1 apex.message.addVisibilityCheck

This function is for APEX region plug-in developers. In order to navigate to items (page items or column items) that have an error (or anything else that can be in an error state), the error item must be visible before it is focused. Any region type that can possibly hide its contents should add a visibility check function using this method. Each function added is called for any region or item that needs to be made visible.

Syntax

apex.message.addVisibilityCheck( pFunction )

Parameters

Table 31-27 apex.message.addVisibilityCheck

Name Type Description

pFunction

Function

A function that is called with an element ID. The function should ensure that the element is visible if the element is managed or controlled by the region type that added the function.

Example

The following example explains a Region plug-in type called Expander, that can show or hide its contents and can contain page items. For purposes of example, this plug-in adds a t-Expander class to its region element and also has an expand method available, to expand its contents.

This region should register a visibility check function as follows:

 apex.message.addVisibilityCheck( function( id ) {
    var lExpander$  = $( "#" + id ).closest( "t-Expander" );

    // Check if lExpander$ is an 'expander' region
    if ( lExpander$.hasClass( "t-Expander" ) ) {

        // If so, expander region must show its contents
        lExpander$.expander( "expand" );
    }
});

31.7.2 apex.message.alert

This function displays an alert dialog with the given message and OK button. The callback function passed as the pCallback parameter is called when the dialog is closed. The dialog displays using the jQuery UI Dialog widget.

There are some differences between this function and a browser’s built-in alert function:

  • The dialog style will be consistent with the rest of the application.

  • The dialog can be moved.

  • The call to apex.message.alert does not block. Any code defined following the call to apex.message.alert will run before the user presses OK. Therefore code to run after the user closes the dialog must be done from within the callback, as shown in the example.

Note:

If either of the following two pre requisites are not met, the function falls back to using the browser’s built-in alert:

  • jQuery UI dialog widget code must be loaded on the page.

  • The browser must be running in Standards mode. This is because if it is running in Quirks mode (as is the case with some older themes), this can cause issues with display position, where the dialog positions itself in the vertical center of the page, rather than the center of the visible viewport.

Syntax

apex.message.alert( pMessage, pCallback )

Parameters

Table 31-28 apex.message.alert

Name Type Description

pMessage

String

The message to display in the confirmation dialog.

pCallback

Function

Callback function called when dialog is closed.

Example

The following example displays an alert Load complete, then after the dialog closes executes the afterLoad() function.

This region should register a visibility check function as follows:

 apex.message.alert( "Load complete.", function(){
    afterLoad();
});

31.7.3 apex.message.clearErrors

This function clears all the errors currently displayed on the page.

Syntax

apex.message.clearErrors()

Parameters

None

Example

The following example demonstrates clearing all the errors currently displayed on the page.

 apex.message.clearErrors();

31.7.4 apex.message.confirm

This function displays a confirmation dialog with the given message and OK and Cancel buttons. The callback function passed as the pCallback parameter is called when the dialog is closed, and passes true if OK is pressed and false otherwise. The dialog displays using the jQuery UI Dialog widget.

There are some differences between this function and a browser’s built-in alert function:

  • The dialog style will be consistent with the rest of the application.

  • The dialog can be moved.

  • The call to apex.message.confirm does not block. Any code defined following the call to apex.message.confirm will run before the user presses OK or Cancel. Therefore acting on the user’s choice must be done from within the callback, as shown in the example.

Note:

If either of the following two pre requisites are not met, the function falls back to using the browser’s built-in confirm:

  • jQuery UI dialog widget code must be loaded on the page.

  • The browser must be running in Standards mode. This is because if it is running in Quirks mode (as is the case with some older themes), this can cause issues with display position, where the dialog positions itself in the vertical center of the page, rather than the center of the visible viewport.

Syntax

apex.message.confirm( pMessage, pCallback )

Parameters

Table 31-29 apex.message.confirm

Name Type Description

pMessage

String

The message to display in the confirmation dialog.

pCallback

Function

Callback function called when dialog is closed. Function passes the following parameter:

  • OK Pressed: true if OK is pressed, false otherwise (if Cancel pressed, or the dialog was closed by some other means).

Example

The following example displays a confirmation message Are you sure?, and if OK is pressed executes the deleteIt() function.

This region should register a visibility check function as follows:

apex.message.confirm( "Are you sure?", function( okPressed ) { 
    if( okPressed ) {
        deleteIt();
    }
});

31.7.5 apex.message.hidePageSuccess

This function hides the page-level success message.

Tip:

As a theme developer, you can influence or override what happens when hiding a page-level success message. For more information, please refer to the apex.message.setThemeHooks function (specifically the beforeHide callback function, where you would need to check for pMsgType === apex.message.TYPE.SUCCESS to isolate when hiding a page-level success message).

Syntax

apex.message.hidePageSuccess()

Parameters

None

Example

The following example demonstrates hiding the page-level success message.

 apex.message.hidePageSuccess();

31.7.6 apex.message.setThemeHooks

This function allows a theme to influence some behavior offered by the apex.message API. You can call this function from theme page initialization code.

Syntax

apex.message.setThemeHooks( pOptions )

Parameters

Table 31-30 apex.message.setThemeHooks

Name Type Description

pOptions

Object

An object with pOptions properties described in the following table.

Table 31-31 pOptions properties

Name Type Description

beforeShow

Function

Callback function that will be called prior to the default show page-notification functionality. Optionally return false from the callback to completely override default show functionality. Callback passes the following parameters:

  • pMsgType: Identifies the message type. Use apex.message.TYPE to identify whether showing an error or success message

  • pElement$: jQuery object containing the element being shown.

beforeHide

Function

Callback function that will be called prior to the default hide page-notification functionality. Optionally return false from the callback to completely override default hide functionality. Callback passes the following parameters:

  • pMsgType: Identifies the message type. Use apex.message.TYPE to identify showing an error or success message

  • pElement$: jQuery object containing the element being hidden.

closeNotificationSelector

String

jQuery selector to identify the close buttons in notifications, defaults to that used by Universal Theme (button.t-Button—closeAlert). May be required by custom themes if you still want to have APEX handle the hide logic, and where messaging contains a close notification button with a different class.

Example

The following example shows beforeShow and beforeHide callbacks defined. To add and remove an additional class animate-msg on the notification element, before the default show and hide logic. This will happen for success messages only by the quality of the check on pMsgType.

Note:

The callbacks do not return anything, therefore the default show / hide behavior happens after the callback.

apex.message.setThemeHooks( 
    beforeShow: function( pMsgType, pElement$ ){
        if ( pMsgType === apex.message.TYPE.SUCCESS ) {
            pElement$.addClass( "animate-msg" );
        }
    }, 
    beforeHide: function( pMsgType, pElement$ ){
        if ( pMsgType === apex.message.TYPE.SUCCESS ) {
            pElement$.removeClass( "animate-msg" );
        }
    }             
}); 

31.7.7 apex.message.showErrors

This function displays all errors on the apex.message error stack. If you do not want to add to the stack, you must first call clearErrors(). Errors display using the current app’s theme’s templates. For page level messages (where location = page), error messages use markup from the page template’s Subtemplate > Notification attribute. For item level messages (where location = inline), error messages use markup from the item’s label template’s Error Display > Error Template attribute.

Tip:

Theme developers must ensure the following:
  • To display errors for your theme, it must define both of the template attributes described above. In addition, for inline errors the label template must reference the #ERROR_TEMPLATE# substitution string in either the Before Item or After Item attributes of your label templates.

  • As a theme developer, you can influence or override what happens when showing page-level errors. For more information, please refer to the apex.message.setThemeHooks function (specifically the beforeShow callback function, where you would need to check for ‘pMsgType === apex.message.TYPE.ERROR’ to isolate when showing page level errors).

Syntax

apex.message.showErrors( pErrors )

Parameters

Table 31-32 apex.message.showErrors

Name Type Description

pErrors

Object or Array

An object, or array of objects with properties described in the following table

Table 31-33 pErrors

Name Type Description

type

String

Must pass error, although may support different notification types in the future.

location

String | Array

Possible values are: inline, page or [inline,page].

pageItem

String

Item reference where an inline error should display. Required when location = inline.

message

String

The error message.

unsafe

Boolean

Pass true so that the message will be escaped by showErrors. Pass false if the message is already escaped and does not need to be escaped by showErrors.

Examples

The following example first clears any existing errors displayed, and then displays two errors. To display the two errors, an array containing two error objects is passed to the showErrors call. The first error message is Name is required! and displays at both page level and inline with the item P1_ENAME. The second error messages is Page error has occurred! and displays just at page level. In both errors, the message text is considered safe and therefore will not be escaped.

// First clear the errors
apex.message.clearErrors();

// Now show new errors
apex.message.showErrors([
    {
        type:       "error",
        location:   [ "page", "inline" ],
        pageItem:   "P1_ENAME",
        message:    "Name is required!",
        unsafe:     false
    },
    {
        type:       "error",
        location:   "page",
        message:    "Page error has occurred!",
        unsafe:     false
    }
]);

31.7.8 apex.message.showPageSuccess

This function displays a page-level success message. This clears any previous success messages displayed, and also assumes there are no errors and clears any errors previously displayed. Success messages display using the current app’s theme’s template. Specifically for page success messages, the markup from the page template’s Subtemplate > Success Message attribute is used.

Tip:

As a theme developer, you can influence or override what happens when showing a page-level success message. For more information, refer to the apex.message.setThemeHooks function (specifically the beforeShow callback function, where you would need to check for pMsgType === apex.message.TYPE.SUCCESS to isolate when showing a page-level success message).

Syntax

apex.message.showPageSuccess( pMessage )

Parameters

Table 31-34 apex.message.showPageSuccess

Name Type Description

pMessage

String

The success message to display.

Example

The following example demonstrates a page-level success message Changes saved!.

 apex.message.showPageSuccess( "Changes saved!" );

31.7.9 apex.message.TYPE

The TYPE property is an object that contains properties (constants) that can be useful when calling some apex.message APIs. For example setThemeHooks.

Properties

Table 31-35 apex.message.TYPE properties

Value Description

SUCCESS: “success”

Identifies a success message

ERROR: “error”

Identifies an error message

Example

See apex.message.setThemeHooks for an example of how this can be used.