You are here

reg_confirm_email.module in Registration Confirm Email Address 7

Same filename and directory in other branches
  1. 8 reg_confirm_email.module

Module file for reg_confirm_email.

File

reg_confirm_email.module
View source
<?php

/**
 * @file
 * Module file for reg_confirm_email.
 */

/**
 * Implements hook_form_alter().
 */
function reg_confirm_email_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'user_admin_settings':

      // Add a checkbox option in account settings page.
      $form['register_settings'] = array(
        '#type' => 'fieldset',
        '#title' => t('Confirm email address'),
        '#weight' => -2,
      );
      $form['register_settings']['reg_confirm_email_option'] = array(
        '#type' => 'checkbox',
        '#title' => t('Use two e-mail fields on registration form'),
        '#default_value' => variable_get('reg_confirm_email_option', 0),
        '#description' => t("User will have to type the same e-mail address into both fields. This helps to confirm that they've typed the correct address."),
      );
      $form['register_settings']['reg_confirm_email_desc'] = array(
        '#type' => 'textfield',
        '#title' => t('Confirm Email Description'),
        '#default_value' => variable_get('reg_confirm_email_desc', NULL),
        '#description' => t("Add help text"),
      );
      break;
  }
}

/**
 * Implements hook_ID_form_alter().
 */
function reg_confirm_email_form_user_register_form_alter(&$form, &$form_state) {
  $mail_confirm = variable_get('reg_confirm_email_option', 0);

  // Show the confirm email field only if it is enabled in settings.
  if ($mail_confirm) {
    $form['account']['conf_mail'] = array(
      '#type' => 'textfield',
      '#title' => t('Confirm e-mail address'),
      '#weight' => -28,
      '#maxlength' => 64,
      '#description' => variable_get('reg_confirm_email_desc', NULL),
      '#required' => TRUE,
    );

    // Weight things properly so that the order is name, mail, conf_mail.
    $form['account']['name']['#weight'] = -30;
    $form['account']['mail']['#weight'] = -29;

    // Add the validation function for this module.
    $form['#validate'][] = 'reg_confirm_email_user_register_validate';
  }
}

/**
 * Custom validation function for user registration form.
 *
 * @see reg_confirm_email_form_user_register_form_alter
 */
function reg_confirm_email_user_register_validate($form, &$form_state) {

  // Check to see whether e-mail address matches the confirm address.
  if (variable_get('reg_confirm_email_option', 0) && isset($form_state['values']['conf_mail'])) {
    if ($form_state['values']['mail'] !== $form_state['values']['conf_mail']) {
      form_set_error('conf_mail', t('Your e-mail address and confirmed e-mail address must match.'));
    }
  }
}

Functions

Namesort descending Description
reg_confirm_email_form_alter Implements hook_form_alter().
reg_confirm_email_form_user_register_form_alter Implements hook_ID_form_alter().
reg_confirm_email_user_register_validate Custom validation function for user registration form.