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?

  • 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 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 at 12:54
  • You don’t need to return anything it is passed by reference. – Kevin Apr 2 at 13:01
  • i have remove return $form still autocomplete is coming – sani Apr 2 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 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.