Skip to content

Instantly share code, notes, and snippets.

@iAugur
Last active July 8, 2019 12:15
Show Gist options
  • Save iAugur/7771036 to your computer and use it in GitHub Desktop.
Save iAugur/7771036 to your computer and use it in GitHub Desktop.
Adding a Reset Button to a Drupal form
I have come across a lot of discussions about how to add a reset button to a form in Drupal 7.
If you search for 'add a reset button to a drupal form' you will find loads of examples using the #markup method of adding html for a reset button. This is because using the render array to add a button #type=> 'button' gives the button a class of 'form-submit'.
But you can use the render array and simply give the button a '#button_type' which translates into a class. Buttons on a form default to submit and that is why they get a class of form-submit. But make the type 'reset' and it will get a class of form-reset.
<?php
// this would be in your Form Alter Hook
// Common suggestion.
$form['reset'] = array(
'#markup' => '<input '. drupal_attributes(array('type' => 'reset', 'value' => t('Reset -') )) .' class="form-reset" />',
);
// But you don't have to use markup to achieve this, you can do it like this:
$form['reset'] = array(
'#type' => 'button',
'#button_type' => 'reset',
'#value' => t('Clear -'),
'#weight' => 9,
'#validate' => array(),
'#attributes' => array(
'onclick' => 'this.form.reset(); return false;',
),
);
?>
@RuZniki
Copy link

RuZniki commented Mar 27, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment