I'm working on a custom login page for users. I have some special elements on the page, which might cause authentication to fail even though a user enters a valid username and password combination.

After a failed authentication attempt, the username field still keeps the username they entered, but the password field is blank. I would like the password field to keep its value, like the username field does.

I'm starting the form with user_login(). I added a check to add in the password:

if (isset($form_state['input']['pass']) ) {
// user has submitted the login form once.

 // maintain their entered password
  $form['pass']['#default_value'] = $form_state['input']['pass'];

Yet it doesn't survive.

How can I re-populate the password field after a failed login?

  • Where did you add that code? Can you provide more context, and code?
    – apaderno
    Aug 9, 2011 at 21:57

3 Answers 3


The password field doesn't use the "#default_value" property. This means that is not possible to pre fill the field with a default value as it is possible with a textfield field. As it is not possible to set a default value for the field, a password field is always shown empty.

It is what I would expect, especially when a validation handler returns an error for the entered password. If the password is wrong, I don't see any reason to pre fill the field with what previously entered, considering that what the user sees is something similar to "•••••" (or a group of asterisks).

  • It would just be nice for a user not to have to enter their password again because they forgot to check a box on another part of the page. ( I didn't create the requirements for this site :)
    – user1359
    Aug 10, 2011 at 13:04
  • I understood that, but still when an error is reported from the validation handler for a password, the error is usually "The password is wrong." If a module, for example, wants to avoid a user uses the same password for more than X times, it should not return a validation error when the password has been used X+1 times; it should allow the user to log in, and then force the user to change the password.
    – apaderno
    Aug 10, 2011 at 13:16
  • Makes sense -- I wasn't thinking about that.
    – user1359
    Aug 10, 2011 at 13:35

IMO it should be:


Anything goes, even though #default_value is really not supported for the password element.

To make it work you'll have to override the default <input type="password"> theming. Then, use hook_form_alter to pass the value from $form_state back to $form.

I got it to work on my side with code like this:


function YOUR_THEME_theme($existing, $type, $theme, $path) {
  return array(
    'password' => array(
      'variables' => array('element' => NULL)

function YOUR_THEME_password($element) {
  $size = $element['#size'] ? ' size="'. $element['#size'] .'" ' : '';
  $maxlength = $element['#maxlength'] ? ' maxlength="'. $element['#maxlength'] .'" ' : '';
  $default_value = $element['#default_value'] ? ' value="'. $element['#default_value'] .'" ' : '';

  _form_set_class($element, array('form-text'));
  $output = '<input type="password" name="'. $element['#name'] .'" id="'. $element['#id'] .'" '. $default_value . $maxlength . $size . drupal_attributes($element['#attributes']) .' />';
  return theme('form_element', $element, $output);


function YOUR_MODULE_form_user_login_alter(&$form, &$form_state) {
  $form['pass']['#default_value'] = $form_state['post']['pass'];

Now I'm not quite sure about the last one, though. I was expecting $form_state['values'] like IMO suggested, but I only have $form_state['post']. While that works, somebody may want to clarify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.