
Autocomplete, clearing form values and Drupal form fields
Some browsers like to be helpful and help by remembering certain form fields. I was having a problem with Firefox auto-populating my some of my form fields (specifically field type 'password' and 'password_confirm') in a custom module I wrote.
After some consternation, I referred to our cardboard cutout Han Solo, who helped me to remember a bit more about form options and field auto-completion.
To fix this problem just add an #attribute of 'autocomplete' => 'off', e.g.
<?php /** * Implementation of hook_form() */ function mymodule_form() { $form['old_password'] = array( '#title' => t('Old Password'), '#type' => 'textfield', '#attributes' => array('autocomplete' =>'off'), '#required' => TRUE, ); $form['new_password'] = array( '#type' => 'password_confirm', '#attributes' => array('autocomplete' =>'off'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), ); $form['clear'] = array( '#type' => 'button', '#value' => t('Clear'), '#attributes' => array('onclick' => 'this.form.reset(); return false;'), ); return $form; }
The example above also includes a nifty way to clear out form fields via javascript in the $form['clear'] element.
Comments
I love talking to Han. We need to get a picture of him up on the website soon.
Tue, 12/07/2010 - 10:27
Thx for sharing this script. I have used some of this in my first drupal module, used for theming the user registration page.
Really like your code for clearing a drupal form. This seems to work well apart from clearing the username and email address fields.
Not sure if this is related but I can;t seem to get the autocomplete part to (not) work. Not sure but I guess this is controlled through the use of sessions and the browser. Also when I looked at drupal's fapi I could see no documentation for this although it did mention about autocomplete_path. Is this the same?
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference…
Wed, 12/15/2010 - 15:47
Hi!
I'm a real dummy in these programming things. So I would really appreciate, if anyone could tell me where/in what file I have to put this script...? Thanx alot in advance
-A-
Wed, 12/15/2010 - 21:30
This is sample code that would go in a module. Here is a tutorial for creating modules in Drupal 6: http://drupal.org/node/206753
Mon, 12/14/2015 - 12:56
Thanks for the Code it really helped
Thu, 03/31/2016 - 10:41
Thanks
Wed, 09/07/2016 - 13:49
Thanks! Very Helpful code
Mon, 02/23/2009 - 10:36