0
<?php
/*
** Implements hook_menu().
*/
function custom_register_menu() {
    $items = array();
    $items['custom-register'] = array(
    'title' => 'Custom register',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('custom_register_form'),
    'access arguments' => array('access content'),
  );
    return $items;
}
/*
** Implements hook_form().
*/
function custom_register_form($form, &$form_state) {
  // Display page 2 if $form_state['page_num'] == 2
  if (!empty($form_state['page_num']) && $form_state['page_num'] == 2) {
    return custom_register_page_two($form, $form_state);
  }
  // Otherwise we build page 1.
  $form_state['page_num'] = 1;
  $form['egn'] = array(
    '#type' => 'textfield',
    '#title' => t('EGN'),
    '#size' => 20,
    '#maxlength' => 10,
    '#required' => TRUE,
  );
  $form['last_digits'] = array(
    '#type' => 'textfield',
    '#size' => 20,
    '#maxlength' => 4,
    '#title' => t('Last digits from the card '),
    '#required' => TRUE,
  );
  $form['authorization_code'] = array(
       '#type' => 'textfield',
     '#size' => 20,
     '#maxlength' => 6,
     '#title' => t('Authorization code'),
     '#required' => TRUE,
  );
  $form['transcation_date'] = array(
       '#type' => 'textfield',
     '#size' => 20,
     '#title' => t('Transaction date'),
     '#required' => TRUE,
  );
  $form['next'] = array(
    '#type' => 'submit',
    '#value' => t('Forward>'),
    '#submit' => array('custom_register_next_submit'),
    '#validate' => array('custom_register_next_validate'),
  );
  //dsm($form_state);
  return $form;
}
/**
* Validate handler for the next button on first page.
*/
function custom_register_next_validate($form, &$form_state) {
    if(!is_numeric($form_state['values']['egn'])) {
        form_set_error('egn', t('Please enter only numbers!'));
    }
    $egn_length = strlen($form_state['values']['egn']);
    if($egn_length == 10) {
    }  else {
        form_set_error('egn', t('Please insert proper ЕГН'));
    }
    if(!is_numeric($form_state['values']['last_digits'])) {
        form_set_error('last_digits', t('Please enter only digits!'));
    }
    $last_digits_length = strlen($form_state['values']['last_digits']);
    if($last_digits_length == 4) {
    }  else {
        form_set_error('last_digits', t('Please enter 4 digits!'));
    }
    $authorization_code_length = strlen($form_state['values']['authorization_code']);
    if($authorization_code_length == 6) {
    }  else {
        form_set_error('authorization_code', t('Please enter 6 digits!'));
    }

I'm building a multistep form for custom registration on Drupal 7. The reason that I don't use hook_form_alter is because later I have to create a submit handler to a webservice and other database, but I'll open a new thread later for that.

This is a part of my code (sorry about putting text the down bellow, but drupal.stackexhange is not so friendly for putting text and codes) and you can see that I've created a menu page (custom-register). On that page there are four fields with custom validators and next button. On the next step it should be displayed the Drupal four fields (username, password, password repeat and email) and after that it should redirect to some register successful message page.

My questions are:

  1. How can I redirect /custom-register to user/register page? I've read about hook_form_alter, but I didn't find a way how to override user/register page.
  2. For the second step of the form after submitting how can I create a new user like on the core user module?

EDIT:

I've managed to alter the default register form with my new form created. :) This is the code:

<?php

    function custom_register_menu_alter(&$items) {
        $items['user/register']['page arguments'] = array('custom_register_form'); 
        return $items;  

    }

Now the next thing is to create the 4 fields (username, password, password repeat and email) and to find a right submit handler to save the user.

1

You can still use hook_form_alter and have a special variable (in the $_SESSION for example) to define which part of the form do you need to show. Code sample below:

function mymodule_form_alter(&$form, &$form_state, $form_id) 
{
  if ($form_id === 'user_register_form') {
     switch ($_SESSION['register_step']) {
         case 1:
              // confirm smth
         break;
         case 2:
              // establish the connections to other services, etc.
         break;
         default:
              // show initial form
         break;
     }
  }
}

Hope this is helpful.

EDIT

Some links:

|improve this answer|||||
  • Thank you for your comment, but I don't get it. If I use hook_form_alter, how can I link the first step from my custom-register form with the core register form, or do you mean I can create a multistep form with hook_form_alter? – user2519032 Jan 12 '14 at 22:19
  • Exactly. Alter the form to create extra steps. You can use the $_SESSION for saving the form state. – t1gor Jan 13 '14 at 15:43
  • Igor, I've tried now with hook_form_alter, but I have some difficulties with it. I've put the new 4 fields from my form above and now the default username, pass, pass repeat and email are displayed too. How can I hide them on the first step and after click on the next button, the default fields to be displayed? I've didn't find any example of multistep form with hook_form_alter. – user2519032 Jan 13 '14 at 18:03
  • If you could show me some code example using my form it will be very nice, so I can understand how to achieve the multistep form in right order. – user2519032 Jan 13 '14 at 18:44
  • I would appreciate if you could tick the answer if it helped :) – t1gor Sep 6 '17 at 7:51
0

I've finally done it! :) This thread https://drupal.org/node/999420 has a great examples how to achieve the multistep form with hook_form_alter.

|improve this answer|||||

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.