Search code examples
oracle-apex

How to validate and submit a modal dialog, and, when succesful, execute some Javascript


The following order of events should happen in my Oracle APEX app:

  1. User clicks Submit button in a modal dialog.
  2. The configured validations are executed (mostly package functions).
  3. If validation is OK, the page is submitted (a package function is called).
  4. If no validation error or exception during submit happened, Javascript is executed. This Javascript needs access to the item values entered by the user, so it must not be executed after a page reload.
  5. Modal dialog is closed.

I tried many ways to accomplish this, but all failed. The obvious solution would be two dynamic actions on the button click, but I learned that the order of the actions is not guaranteed, and the Javascript is executed even after an error in the validation or submit.

Now I think I have to do it with Javascript in a single dynamic action (button click on the modal dialog) like this:

// Validate and submit.
apex.page.submit( {
    validate: true,
} );

// Other JS code on successful submit, which is accessing page items.
... $v("myItem") ...

// Close modal dialog.
apex.navigation.dialog.close(true);

How can I check if apex.page.submit was successful? And will the javascript be able to access item values entered by the user?

By the way, I don't want to put this Javascript on the parent page, because there will be multiple such modal dialogs, and I prefer to keep their respective logic separate, and not turn the parent page into a "God object".

Thank you in advance.


Solution

  • This isn't an answer to your question, because I believe what you want is simply not possible. To understand why, you need to check how APEX processes work.

    1. A number of processes run in a pre-rendering phase. These are server-side processes (computations, pl/sql processes, form initialization, etc).
    2. The dom is rendered based on the components. These components (optionally) take data from the pre-rendering process. In this phase client-side actions can be executed (dynamic actions, custom javascript)
    3. Once a page is submitted, the "processing" starts. The form data entered in the previous phase it sent to the server. From this point onward everything is server-side: validations, page processing, branches, etc).

    Now... javascript is client side only. and only in phase 2 can client-side code be executed. Once a page is submitted, the client can no longer be accessed. So "execute javascript" after "server side validations" is simply not possible in APEX. javascript can be executed onload OR when the page is rendered via an event OR before page submit. That's it in the current version of apex (23.1).