-1

I know that my Drupal installation (9.3.15) doesn't trim spaces at the login page (form id: user-login-form). I have some code in the HOOK_form_user_login_form_alter but in that place in $form_state I don't have any ->getValue('%field%') , it returns NULL. Also I can not modify '$form['name']['#value']' because it is empty in that context.

How do I trim spaces on the user's input? I can do $form_state->getUserInput(); and I can see what the user has entered but I don't know how to modify it. Because $form_alter is an object.

1 Answer 1

1

In your_module.module

  1. You create form alter if there is no any (e.g. HOOK_form_FORM_ID_alter())
  2. you add a validation callback to a field of the whole form (e.g. array_unshift($form['#validate'], 'trim_login_credentials');)
  3. create function trim_login_credentials(&$form, $form_state) {}
  4. add your validation or string operations into that the newly created function;

I do 'array_unshift()` because I want my validation before Drupal's.

Another way

A question at stackoverflow

2
  • This doesn't answer the I can do $form_state->getUserInput(); and I can see what the user has entered but I don't know how to modify it. part.
    – avpaderno
    Commented Jun 10, 2022 at 15:00
  • @apaderno apparently you don't do anything with that user input. It was the only place where you can see what a user has entered, but it has passed the validation at that stage so no matter what you do with $form_state - it's too late to trim spaces. In the beginning, I thought there is something I can do with it. The question is - how to trim spaces. And there are no good questions on the internet about this
    – mevsme
    Commented Jun 10, 2022 at 18:09

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