If two submit buttons have same values, the first button will be treated as the second button..

/**
 * Problems:
 * When I click the first 'Submit' button, it shows "Hello1"!
 * I.e, when I click the first button, drupal think treats the trigger
 * button as the second submit button!
 * If I change the first button's value to "Button", or whatever different
 * from the second button's value "Submit", then it works fine.
 */

function agl_jira_new_test_form($form, &$form_state) {

  $form['textfield_1_1'] = array(
    '#title' => t('name'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['textfield_2'] = array(
    '#title' => t('name2'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );

  $form['button'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#limit_validation_errors' => array(array('textfield_1_1')),
    '#submit' => array('agl_jira_new_test_form_submit'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#submit' => array('agl_jira_new_test_form_submit1'),
  );
  return $form;
}
function agl_jira_new_test_form_submit($form, &$form_state) {
  // do something.
  dsm('Hello');
  $form_state['rebuild'] = TRUE;
}

function agl_jira_new_test_form_submit1($form, &$form_state) {
  // do something.
  dsm('Hello1');
  $form_state['rebuild'] = TRUE;
}

Any help will be appreciated!

Comments

smiletrl’s picture

Thanks to the help of @ivanjaros from IRC, adding a custom '#name' property for the first submit button solves the problem. Drupal's default '#name' for submit button is 'op'. So we need to override this to make multiple buttons work, when they have same values.

heyddi’s picture

Great solution ! Thanks a lot

LeonKing’s picture

Even if I put custom names on my submit button. It still does not work:

$form['action_inline'] = array(
            '#type' => 'container',
            '#attributes' => array(
                'class' => array('form-action', 'inline-buttons'),
            ),
        );

        $form['action_inline']['finish'] = array(
            '#type' => 'submit',
            '#name' => 'finish',
            '#value' => $this->translate('finish_label'),
            '#submit' => $this->submit('finish'),
            '#attributes' => array(
                'class' => array('btn btn-flexible'),
            ),
            '#access' => false,
        );

        $form['action_inline']['cancel'] = array(
            '#type' => 'submit',
            '#name' => 'cancel',
            '#value' => $this->translate('cancel_label'),
            '#submit' => $this->submit('cancel'),
            '#attributes' => array(
                'class' => array('btn btn-flexible'),
            ),
            '#limit_validation_errors' => [],
            '#access' => false,
        );

(Just a bit custom code there but still works the same as Drupal's basic.)

jaypan’s picture

The 'Drupal' way around this is to implement hook_forms(), but your solution also works fine.


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
smiletrl’s picture

Thanks for the head up!

Yeah, hook_forms could be used too. My real usage cases is a bit complicated though:)