0

I am trying to override default reset password functionality by using hook_form_FORM_ID_alter as below: The below code is written in custom module called usr_registration.module file

    /**
     * Implements hook_form_FORM_ID_alter().
     */
    function usr_registration_form_user_pass_reset_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id, AccountInterface $user = NULL, $expiration_date = NULL, $timestamp = NULL, $hash = NULL) { 

            $form['account']['pass'] = [
            '#type' => 'password_confirm',
            '#size' => 25,
            '#description' => t('Provide a password for the new account in both fields.'),
            '#required' => TRUE,];

             $form['#validate'][] = 'usr_registration_validate';
             $form['actions']['submit']['#submit'][] = 'usr_registration_form_user_pass_reset_custom_submit';
    }

    // this function itself is not getting called
    function usr_registration_validate(&$form, FormStateInterface $form_state) {

      //unable to see below debug message and form is getting submitted
       echo "I am here"; exit;

       if($never_login) {
                        //some custom logic
       } else {
                       //some custom logic
       }

     }  

The important thing I noticed in UserPasswordResetForm class(user core module's form) is that there is no default validate method defined. I am unable to find out that why I cant alter the UserPasswordResetForm and custom validate handler over here.

note: My question is similar to this but not exactly same so please dont mark it as redundant.

3

$form['#validate'] will not work as that form "This form works by submitting the hash and timestamp to the user.reset" aka redirects you to the user.reset from

what you can do is something like temp replace this form with a custom one though. however i don't really see the point

/**
 * Implements hook_form_alter().
 */
function yourmodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id === 'user_pass_reset') {
    $action = $form['#action'];
    // Set the original action.
    $form_state->set('original_action', $action);
    // temp replace from with custom one. 
    $form = \Drupal::formBuilder()->buildForm('\Drupal\yourmodule\Form\AlterTheLoginResetFrom', $form_state);
    return $form;
  }
}

then do

class AlterTheLoginResetFrom extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'alter_the_login_reset_from';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    // Get the stuff you need from ... 
    dump($form_state->get('original_action'));
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    foreach ($form_state->getValues() as $key => $value) {
      // @TODO: Validate fields.
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // redirect to original as uses get 
  }
}

or alter the user.reset directly see Drupal\user\Form\UserPasswordResetForm; line 60 & 71 and see how core does it (resetPassLogin) \Drupal\user\Controller\UserController::resetPassLogin

if all else fails perhaps just validate the passwords in js.

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.