0

I want to alter my default user login form into multi step. therefore I have wrote a code which I'm pasting below. but this code is not working, whenever I click on "Next" button it will return on the first step. please help me to get rid out of this problem. Here is my code:

function signin_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_login' || $form_id == 'user_login_block') {
    unset($form['name']);
    unset($form['pass']);
    unset($form['actions']);
    $form['mail'] = [
      '#title'       => t('Email'),
      '#description' => t('Enter your Email'),
      '#type'        => 'textfield',
      '#required'    => 'true',
      '#maxlength'   => 40,
    ];
    $form['next'] = [
      '#name'   => 'next',
      '#value'  => t('Next'),
      '#type'   => 'submit',
      '#submit' => ['signin_step1_form_submit'],
    ];
    $form['#validate'] = ['signin_step1_form_validate'];
  }
}

function signin_step1_form_validate($form, &$form_state) {
  $mail = $form_state['values']['mail'];
  if (empty(user_load_by_mail($mail))) {
    drupal_set_message(t('The entered email is not associated with any account.'));
  }
}

function signin_step1_form_submit($form, $form_state) {
  $form_state['stored_values']['mail'] = $form_state['values']['mail'];
  return signin_step2_form($form, $form_state);
}

function signin_step2_form($form, &$form_state) {
  $form['pass'] = [
    '#title'       => t('Password'),
    '#description' => t('Enter the password'),
    '#type'        => 'password',
    #required' => 'true',
  ];
  $form['signin'] = [
    '#name'   => 'signin',
    '#value'  => t('SignIn'),
    '#type'   => 'submit',
    '#submit' => ['signin_step2_form_submit'],
  ];
  $form['#validate'] = ['signin_step2_form_validate'];
}

function signin_step2_form_validate($form, &$form_state) {
  $mail = $form_state['stored_values']['mail'];
  $user_obj = user_load_by_mail($mail);
  $name = $user_obj->name;
  $pass = $form_state['values']['pass'];
  if (!user_authenticate($name, $pass)) {
    drupal_set_message('error', t('The Password you entered is incorrect'));
  }
}

function signin_step2_form_submit($form, &$form_state) {
  $mail = $form_state['stored_values']['mail'];
  $user_obj = user_load_by_mail($mail);
  $form_state['uid'] = $user_obj->uid;
  user_login_submit($form, $form_state);
}
0

Finally I got it to worked. here is my code, I'm pasting this here to saves someone time.

function signin_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_login' || $form_id == 'user_login_block') {
    unset($form['name']);
    unset($form['pass']);
    unset($form['actions']);
    if (isset($form_state['step2'])) {
      $form = signin_step2_form($form, $form_state);
      return $form;
    }
    $form['mail'] = [
      '#title'       => t('Email'),
      '#description' => t('Enter your Email'),
      '#type'        => 'textfield',
      '#required'    => 'true',
      '#maxlength'   => 40,
    ];
    $form['next'] = [
      '#name'   => 'next',
      '#value'  => t('Next'),
      '#type'   => 'submit',
      '#submit' => ['signin_form_submit'],
    ];
    $form['#validate'] = ['signin_step1_form_validate'];
    return $form;
  }
}

function signin_step1_form_validate($form, &$form_state) {
  $mail = $form_state['values']['mail'];
  if (empty(user_load_by_mail($mail))) {
    form_set_error('mail', t('The entered email is not associated with any account.'));
  }
}

function signin_step2_form($form, $form_state) {
  $form['pass'] = [
    '#title'       => t('Password'),
    '#description' => t('Enter the password'),
    '#type'        => 'password',
    #required' => 'true',
  ];
  $form['signin'] = [
    '#name'   => 'signin',
    '#value'  => t('SignIn'),
    '#type'   => 'submit',
    '#submit' => ['signin_form_submit'],
  ];
  $form['#validate'] = ['signin_step2_form_validate'];
  return $form;
}

function signin_step2_form_validate($form, &$form_state) {
  $mail = $form_state['stored_values']['mail'];
  $user_obj = user_load_by_mail($mail);
  $name = $user_obj->name;
  $pass = $form_state['values']['pass'];
  if (!user_authenticate($name, $pass)) {
    form_set_error('pass', t('The Password you entered is incorrect'));
  }
}

function signin_form_submit($form, &$form_state) {
  if ($form_state['clicked_button']['#name'] == 'next') {
    $form_state['step2'] = TRUE;
    $form_state['stored_values']['mail'] = $form_state['values']['mail'];
    $form_state["rebuild"] = TRUE;
  }
  else {
    $mail = $form_state['stored_values']['mail'];
    $user_obj = user_load_by_mail($mail);
    $form_state['uid'] = $user_obj->uid;
    user_login_submit($form, $form_state);
  }
}
1
  • What's the difference to the code from your question? Could you maybe add some more documentation to it?
    – leymannx
    Oct 21 '18 at 5:42

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.