0

I would like to remove auto fill user name and password while login or forgot password.

I have tried this but its not working:

if ($form_id == 'user-login-form') {
  $form['name']['#attributes']['autocomplete'] = 'off';
} 

What am I doing wrong?

13
  • Form IDs will have an _ not a - so you need to use user_login_form If you do print $form_id; within your form_alter function each form id will appear next to every form on the page allowing you to see its proper form id
    – Leigh
    Apr 2, 2019 at 12:23
  • yes you are right form id is user_login_form function but still its not working my function is like this use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; myuser_form_alter(&$form, FormStateInterface $form_state, $form_id) { if ($form_id == 'user_login_form') { $form['name']['#attributes']['autocomplete'] = 'off'; } return $form; } what i'm missing
    – sani
    Apr 2, 2019 at 12:54
  • 1
    You don’t need to return anything it is passed by reference.
    – Kevin
    Apr 2, 2019 at 13:01
  • i have remove return $form still autocomplete is coming
    – sani
    Apr 2, 2019 at 13:15
  • Are you debugging it? Is this the right form item to alter? Is this code even firing? Is the module enabled?
    – Kevin
    Apr 2, 2019 at 13:16

3 Answers 3

0

You can try:

$form['account']['name']['#attributes']['autocomplete'] = 'off';
1
  • 2
    The UserLoginForm class doesn't use $form['account']['name'] but $form['name'].
    – apaderno
    Feb 20, 2020 at 15:41
0

Your problem has nothing to do with Drupal - this is a browser issue. "autocomplete" is simply a suggestion to the browser, and the browser does not have to abide by that suggestion. Modern browsers (for at least 6 years now) have ignored it for username/password fields.

Search the web for "autocomplete=off not working" and you will find explanations and work-arounds.

As far as the hook_form_alter() goes, the simplest thing is to look at the generated HTML page for that form and see if you successfully added the autocomplete="off" attribute to the form tag. If it's not there, then you chose the wrong form ID, or you chose the wrong form element, etc. But as I said, even when you get the hook_form_alter() correct, the browser will probably ignore this attribute and you will have to use one of the work-arounds if you want this to happen.

0

Many modern browsers do not support autocomplete="off" for login fields https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion#the_autocomplete_attribute_and_login_fields

If you still want to implement for browsers that allow, it this is an option for Drupal 9.

function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
   if ('user_login_form' == $form_id) {
    $form['#attributes']['autocomplete'] = 'off';
    $form['pass']['#attributes'] = ['autocomplete' => 'off'];
    $form['name']['#attributes'] = ['autocomplete' => 'off'];
  }
}

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.