In Drupal 7 form API - How do I create an input of type "button" (not "submit")?
Asked Answered
M

6

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".

Meredeth answered 3/1, 2011 at 13:57 Comment(3)
This doesn't work? api.drupal.org/api/drupal/…Administrative
Nope. To quote from the page: "When the button is pressed, the form will be submitted to Drupal, where it is validated and rebuilt."Meredeth
Oh. I suppose its default behavior is to make the button an action, and not just a button button.Administrative
M
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.

Mauchi answered 6/1, 2011 at 20:38 Comment(0)
A
13

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()
Ambry answered 26/4, 2011 at 15:15 Comment(1)
This was such a simple and effective solution!Seamy
S
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

Sumptuous answered 21/1, 2015 at 16:31 Comment(0)
M
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.

Mauchi answered 6/1, 2011 at 20:38 Comment(0)
G
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']));
Galliot answered 21/2, 2012 at 14:29 Comment(1)
To add to my earlier example - that is the Drupal 6 version. In Drupal 7, change the drupal_render to renderGalliot
A
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',
  );
Anglophobe answered 4/8, 2011 at 22:55 Comment(0)
P
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"');

Paving answered 2/4, 2013 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.