Hook into Drupal registration and validate user info against business logic

前端 未结 4 702
粉色の甜心
粉色の甜心 2021-02-06 02:10

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

4条回答
  •  生来不讨喜
    2021-02-06 02:54

    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'));
      }
    }
    

提交回复
热议问题