Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By AltaVida on
I can't find anything in the docs about creating a "reset" button on a form using the new forms API in 4.7
I've tried:
'#type' => 'reset'
'#type'=>'button', '#button_type' => 'reset'
And anything else I could think of. I also searched the form.inc file for the words "clear" or "reset" and came up empty handed...
Comments
I think you're right
Just looking at the APi, i don't see an obvious way to do it, and I agree that's a problem. However, take a look at node.module and node_filter_form_submit(). In setting up the admin/content page there seem to be multiple submit buttons.
for example:
which is then used:
Probably if you have a "reset" button that causes no action upon submission, you'd effectively get a reset. However, this isn't going to work (I think) if there are any required fileds in the form. Hmmm.
Also, take a look at node_page(), which is where the "Delete" button's action gets processed.
---
Work: BioRAFT
Eurka!
I think you can just stick the markup in your form for a reset button where you want it:
Maybe not kosher, but I don't see any other way.
---
Work: BioRAFT
Thanks this worked for me :)
Thanks this worked for me :)
I love Drupal. Always.
Reset button
The code is working fine,it reseting all webform fields but the problem is that the Reset Button is displaying at every page wherever we had a textbox field.How to get rid off with such problem.
reset is archaic
When, in real life, have you ever had the need for a button to wipe away everything you just entered in a form?
You can refresh the page, push the back button, or close the window if you've decided you really don't want to save the stuff you've entered so far.
Reset is pointless. It's a widget left over from the early days of the web and has no place in a world of dynamic web applications.
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Real life example
I agree that 99% of the time it's not needed, but what about this situation:
Go to Google, click on 'advanced search', enter some miscellaneous options and perform the search. Once you're done reviewing the results, and you want to do a different (advanced) search, click on the 'advanced search' link again. Note: your form is pre-filled with your previous search criteria. You now have to manually reset everything you don't need. A reset button would make sense here IMHO, because there is no obvious way to start a fresh new search (unless I'm missing something). For that matter, check out how many options you can fill on Yahoo's advanced search page.
This example is identical to my situation: users can return to advanced search to tweak their previously entered search or start a new one. I can't see how making users manually clear all the fields, reset the dropdowns and uncheck checkboxes does them any favors. If you have an alternative suggestion I'd like to hear it.
Anyways, here's how I did it (using an image button btw):
reset is not clear
You'd think so, wouldn't you?
But look at the HTML.
What 'Reset' the HTML widget does is reset the values to the ones specified in the page code. It doesn't 'clear', it returns to the original state. Client-side.
SOooo. If your helpful search interface is re-populating the fields with your last search, a traditional reset button will not help.
That's maybe why they (and any state-aware html form application) leave it off.
I agree that a 'clear' button would be useful in your example, but it would have to be scripted, not the HTML1.0 thing.
So, your onclick example is nice, and mostly correct, but (unless things are implimented wrongly) the reset() function should re-set the field to the value it was in when the page loaded. If your form is anything like the 'Advanced Search' examples you gave, You may be wanting to loop and set element.value="";
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
It makes sense now...
Ahhhh. Okay that makes sense. I would have to loop through and reset all the values to null as you said. Thanks for taking the time to explain that.
Yes, 'reset' is truly useless!
hi,
hi,
can you please help me how to loop through and reset all the values to null ? do we need to write onclick function?
sorry im new to it, can you please tell me how the syntax will be?
My code is ,
$form['actions']['reset']= array(
'#type' => 'submit',
'#value' => 'reset'
);
Well not really
Reset is not used at all on the consumer side. But lets face it, for admin forms, reset is pretty valuable. Data entry by some marketing temp person will pretty much require the reset button a couple of times. Refresh is a good idea, except that it takes time to reload the page, soooooooooooo it's a bit easier on the backside. Which is actually the only reason I am looking at this topic, making a product entry form.
How about
... if you need to reset exposed filters? A reset button makes sense then.
So as to get exactly the
So as to get exactly the same effect as pressing 'refresh'?
reset will set your filters to the way they were when you loaded the page - it will not clear them.
.dan.
.dan. is the New Zealand Drupal Developer working on Government Web Standards
phptemplate wil help you
you may use for defining custom button types your own TEMPLATENAME_button function (place it in template.php file). Get all the code from form.inc (theme_button) under /includes and put it in your template.php. Simple check of element type will give you button type you want.
Reset button - How To
Well, I was a little discouraged when reading this post, but with the information posted here and some trial and error I managed.
Using the breadcrumb to reload the page worked fine so this wasn't needed - as stated in the post, but I was asked to add it and needed to find a way. Noticing that the node module uses multiple form buttons, I used that as a template.
Basically add the button to the form, and add an #after_build property to the form
$form['#after_build'] = array('node_form_add_reset');
In the function designated by #after_build, you can see what button was pressed
Here, I am simply reloading the page.
And here is the complete code - note, this form is being rendered at the top of a page so it is a little different.
Hope this helps!
My personal Drupal site.
http://www.regx.dgswa.com/html/index.php
clear/reset button
I have been looking for this button
http://SocialNicheGuru.com
Delivering inSITE(TM), we empower you to deliver the right product and the right message to the right NICHE at the right time across all product, marketing, and sales channels.
here's another reset
here's another reset button
this will restore the original values of the form when the page was loaded
this produces error in internet explorer 8.
$form['clear'] = array(
'#name' => 'clear',
'#type' => 'button',
'#value' => t('Reset'),
'#attributes' => array('onclick' => 'this.form.reset(); return false;'),
);
this produces error in internet explorer 8.
Error Details:
this.form is null or not an object