Last active
July 8, 2019 12:15
-
-
Save iAugur/7771036 to your computer and use it in GitHub Desktop.
Adding a Reset Button to a Drupal form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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;', | |
), | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Drupal 8 you can use this https://gist.github.com/RuZniki/099883da38fdc86614051705e3f4370f