10

I am trying to have a button which is not a "submit" type of button, but rather a normal "button" type, using the forms api of drupal 7, but I can't seem to get it.

I've tried many things, like setting #type to 'button', setting #button_type to 'button' but no matter what I do, drupal always creates a button of type "submit".

Doron
  • 3,176
  • 7
  • 35
  • 60
  • This doesn't work? http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#button – Kevin Jan 03 '11 at 14:04
  • Nope. To quote from the page: "When the button is pressed, the form will be submitted to Drupal, where it is validated and rebuilt." – Doron Jan 03 '11 at 14:19
  • Oh. I suppose its default behavior is to make the button an action, and not just a button button. – Kevin Jan 03 '11 at 15:08

6 Answers6

12

You can use:

"#executes_submit_callback" => FALSE

To disable the "submit" step.

If you only want to disable the "validate" step, use:

"#limit_validation_errors" => array()
J. Costa
  • 7,442
  • 4
  • 26
  • 31
3

In Drupal 7 this can be accomplished by adding:

'#attributes' => array('onclick' => 'return (false);'),

to your button definition. For example:

$form['my_form'] = array(
 '#type' => 'button',
 '#attributes' => array('onclick' => 'return (false);'),
 '#value' => t('My Button'),
 '#prefix' => t('<div class="myButton">'),
 '#suffix' => t('</div>')
);

This worked for my application.

Reference: https://www.drupal.org/node/283065 under Disabling and Overriding Buttons

Susanne
  • 41
  • 1
2

You may want to check out this issue for some background and then consider this workaround. You might also be able to use #markup to insert it manually.

Matt V.
  • 9,703
  • 10
  • 35
  • 56
2

A very simple side-step is the following in your form

$form['your-form-element'] = array(
    '#type' => 'button',
    '#name' => 'any-name',
    '#value' => t('Button Text'),
);

And in your form's template:

print str_replace('type="submit"', 'type="button"', drupal_render($form['your-form-element']));
badzilla
  • 75
  • 1
  • 3
  • To add to my earlier example - that is the Drupal 6 version. In Drupal 7, change the drupal_render to render – badzilla Sep 27 '12 at 09:53
1

Add the following function in your template's template.php file.

function templatename_button($variables) {
  $element = $variables['element'];
  $type = strtolower($element['#button_type']);
  switch($type){
    case 'submit':
    case 'reset':
    case 'button':
      break;
    default:
      $type = 'submit';
      break;
  }
  $element['#attributes']['type'] = $type;

  element_set_attributes($element, array('id', 'name', 'value'));

  $element['#attributes']['class'][] = 'form-' . $element['#button_type'];
  if (!empty($element['#attributes']['disabled'])) {
    $element['#attributes']['class'][] = 'form-button-disabled';
  }

  return '<input' . drupal_attributes($element['#attributes']) . ' />';
}

and in your form

  $form['mybutton'] = array(
    '#type'  => 'button',
    '#value' =>  t('mytext'),
    '#button_type' => 'button',
  );
Vincent
  • 11
  • 1
0

Sometime we need define a default button to submit a form, but all button elements (#type=button,submit) in drupal, the TYPE attribute always is "submit", so must to modify this attribute to "button" that specify a default button we needed.

  1. Render the form element and replace TYPE attribute.

    echo strtr(drupal_render($form['btn']), array('type="submit"' => 'type="button"'));

  2. Modify the form definition.

    form['btn']['#attributes'] = array('onclick' => 'this.type="submit"');

Wouter J
  • 41,455
  • 15
  • 107
  • 112