Here is the setup. I have stripped down and simplified the code snippets for readability, but it should be reproducable.
I have a form with some elements and among them two submit buttons.

'validation' => array(
  '#type' => 'submit',
  '#default_value' => t('Send code'),
  '#name' => 'submit-send-code',
),
'submit-next' = array(
  '#type' => 'submit',
  '#default_value' => t('Next'),
  '#name' => 'submit-next',
),

When 'submit-send-code' is pressed, the submit function sends out a text message with a code and some js unhides a text field for code input. When 'submit-next' is pressed the submit function checks the given code and if it is accepted, the user is sent to the next page. I use the values in $form_state['triggering_element'] to differentiate between these two options.
This setup works great.

However, I want the user to be able to skip validation, but if they do they'll be met with a js prompt that ask if they are certain they want to do that:

(function ($) {
  Drupal.behaviors.validation = {
    attach: function (context, setting) {
      $('input[name=submit-next]').on( 'click', function () {
        var noValidation = // checks for no validation
        if (noValidation) {
          if (!confirm('Are you sure you want to continue without validation?')) {
            return false;
          }
        }
      });
    }
  };
})(jQuery);

This works well also, but here is the kicker: If a user press Next, sees the message, change their mind and go back to press Send Code; then they are sent to the next page without validation. When inspecting the $form_state array the triggering_element sure enough is set to 'submit-next' even though 'submit-send-code' was the last button to be pressed.

It's if the javaScript 'confirm' is preventing Drupal from changing the triggering element.

Is there any fix or workaround for this?

EDIT: I've looked around quite a bit before posting this. I found some possible workarounds in this thread: http://stackoverflow.com/a/6521104/1685346
However, #1 doesn't work, you can't see the triggering element in $_POST, #2 is not feasible in this case and #3 is possible, but very ugly.
Another workaround I'm contemplating is to skip the javascript completely and restructure the form to use set_form_error in the validation and the update the form array, but that seems ugly as well and possibly confusing for the user.
Suggestions are very welcome.

My current environment:
Win 10
EasyPHP vc9
PHP 5.4.24
Drupal 7.41

Comments

Nicolaj created an issue. See original summary.

nicolaj.knudsen@gmail.com’s picture

Issue summary: View changes

Version: 7.41 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.