9

I want to hook into the registration module. I already have a database of 50000 users who use my old website. Now I am migrating to Drupal.

I still haven't migrated the entries to drupal database. I will be checking against my old database.

When a user tries to register in Drupal, I need to check whether the username he gave is already present in that list of 50000 (and growing) entries. If it exists, I need to cancel the registration showing an error msg saying username exists..

Which hook should I use? If my code figures that the validation failed, How can I tell drupal to display an error msg?

Edit: I hooked into the hook_user and checked for the 'validate' op. I am able to validate and assign error messages. But it is happening for all forms. I want to validate only the new account creation form. How can I do that?

Thanks.

user266803
  • 958
  • 1
  • 7
  • 9

4 Answers4

18

You should register an additional validation callback function for the registration form using hook_form_FORM_ID_alter(), somewhat like so:

// Alter the registration form
function yourModuleName_form_user_register_alter(&$form, &$form_state) {
  // Add your own function to the array of validation callbacks
  $form['#validate'][] = 'yourModuleName_user_register_validate';
}

// Perform your own validation
function yourModuleName_user_register_validate($form, &$form_state) {
  // Extract the submitted name
  $name = $form_state['values']['name'];
  // Check it according to your own logic
  $is_valid_name = your_check_for_valid_name();
  // File error, when not valid
  if (!$is_valid) {
    form_set_error('name', t('Name already taken, please choose a different one'));
  }
}
Henrik Opel
  • 19,138
  • 1
  • 45
  • 61
  • 1
    +1 Thanks a lot. It worked, I am able to set the validation for that particular form's field(s). I didn't know about the FORM_ID thing, I used some other hook and it was running for all forms :D – user266803 Feb 20 '10 at 16:14
  • Although this is a great solution, keep in mind that currently Drupal runs hook_form_alter AFTER hook_form_FORM_ID_alter. So there may be a case down the road where another module will over-ride your hook_form_FORM_ID_alter changes. 95% of the time you wont run into a problem, but some say it is safest to use hook_form_alter and a switch statement. – Erik Ahlswede Mar 03 '10 at 21:06
  • If your validation function is only checking a single field, consider creating an [Element Validation](https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#element_validate) function instead. – rymo Jun 11 '13 at 15:51
6

Henrik Opel answer work on Drupal 6. For Drupal 7 use yourModuleName_form_user_register_form_alter

Camilo
  • 2,674
  • 2
  • 26
  • 43
1

Here are some examples for Drupal 7:

/**
 * Implements of hook_user_insert().
 */
function foo_user_insert(&$edit, $account, $category) {
  // foo_user_submit($edit, $account);
}

/**
 * Implementation of hook_user_delete().
 */
function foo_user_delete($account) {
  // foo_user_delete($account);
}

/**
 * Implements hook_form_FORM_ID_alter().
 * Form ID: user_register_form
 */
function foo_form_user_register_form_alter($form, &$form_state) {
  if ($form['#user_category'] == 'account' && !isset($form['#user']->uid)) {
    // Foo code
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 * Form ID: user_profile_form
 */
function foo_form_user_profile_form_alter($form, &$form_state) {
  // Set a custom form validate and submit handlers.
  $form['#validate'][] = 'foo_account_validate';
  $form['#submit'][] = 'foo_account_submit';
}

/**
 * Implements of hook_form_alter().
 * This is the same as: hook_form_FORM_ID_alter()
 */
function foo_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case "user_profile_form":
    case "user_register_form":
      break;
  }
}
kenorb
  • 118,428
  • 63
  • 588
  • 624
0

Consider using the Username Originality AJAX check module: https://www.drupal.org/project/username_check

Darrell Duane
  • 3,785
  • 1
  • 16
  • 8