Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 525
Doron Avatar asked Jan 03 '11 13:01

Doron


3 Answers

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()
like image 152
J. Costa Avatar answered Nov 04 '22 03:11

J. Costa


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

like image 3
Susanne Avatar answered Nov 04 '22 03:11

Susanne


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.

like image 2
Matt V. Avatar answered Nov 04 '22 03:11

Matt V.