<?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:
- 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.
- 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.