Drupal

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.


Filed under:

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.

Similar posts

Get notified on new marketing insights

Be the first to know about new B2B SaaS Marketing insights to build or refine your marketing function with the tools and knowledge of today’s industry.