0

I have a register form that is output in a controller using the following code.

 $form = $this->getUserProfileForm();

    return [
      '#theme' => 'maubl_user_inscription',
      '#form' => $form,
    ];
  }

  protected function getUserProfileForm() {
    $entity = \Drupal::service('entity.manager')
      ->getStorage('user')
      ->create(array());

    $formObject = \Drupal::service('entity.manager')
      ->getFormObject('user', 'default')
      ->setEntity($entity);

    $form = \Drupal::formBuilder()->getForm($formObject);

    return $form;
  }

In a custom module, I'm using the following code to alter the password.

function maubl_user_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $formState) {
  $form['actions']['submit']['#submit'][] = 'maubl_user_form_login_after_registration';
  $form['account']['pass']['#process'][] = 'maubl_user_form_user_register_form_process_pass';
}

function maubl_user_form_user_register_form_process_pass(&$element, \Drupal\Core\Form\FormStateInterface $form_state, &$complete_form) {
  $element = \Drupal\Core\Render\Element\PasswordConfirm::processPasswordConfirm($element, $form_state, $complete_form);
  $element['pass1']['#placeholder'] = t('Password');
  $element['pass2']['#placeholder']= t('Confirm pwd');
}

Yet, any password would be accepted as long as they are the same. I want something like password_confirm to check if there are special characters, special length etc, but I couldn't figure out how to make it works.

When I try to use the following code, nothing happens.

function mymodule_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['account']['pass']['#process'][] = 'mymodule_form_user_register_form_process_pass';
}
5

It seems like you might want to look into the Password Policy module which probably has a lot of the features you are trying to rebuild.

I also wonder whether it makes sense to build your own user inscription form instead of altering the existing core registration form. Altering forms tends to result in easier integrations with existing modules.

| improve this answer | |
  • Thank you for your answer, I actually did try to use the core registration form, but for a reason that I ignore it does not work correctly, I'm using core's one for my pwd reset, my login form but registration didn't work out. I tried to look for a doc but couldn't find something that fits my request. I did try with drupal/web/core/modules/user/src/RegisterForm.php, since there isnt one in Form folder – StefanYYC Dec 11 '19 at 8:28

Your Answer

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

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