0

I added an extra user registration validation critera in my custom module by implementing hook_user

if ('validate' == $op && 'user_register' == $edit['form_id'] && 'account' == $category && !$edit['remote_create_user']) {

    // Is the mail address OK?
    if ($email_in_use) {
    // No, set error on mail form field
    form_set_error('mail', t('Your email address is already associated with a account.'));
    }

I added the !$edit['remote_save'] because I don't want this extra validation if I am creating accounts programmatically. The code I am using to create accounts is

 $form_state['values'] = $account;
    $form_state['values']['pass'] = array(
      'pass1' => $account['pass'],
      'pass2' => $account['pass'],
    );
    $form_state['values']['op'] = t('Create new account');
    $form_state['values']['remote_create_user'] = TRUE;
    $ret = drupal_execute('user_register', $form_state);

As you can see, I tried to add an extra form element called 'remote_save' $form_state['values']['remote_save'] = TRUE;. However, in the hook code above $edit['remote_save'] is empty so I can't use it to stop the extra validation. How can I pass a value from $form_state to the $edit array? Or is there a better way to do this?

EDIT: So I tried to use hook_form_alter with the code below

function module_form_alter(&$form, &$form_state, $form_id)  { 
  if ($form_id == 'user_register') {
    $form['remote_create_user'] = array('#type' => 'value', '#value' => FALSE);
  }
}

However this code $form_state['values']['remote_create_user'] = TRUE; which is in a separate module, does not set the value of $edit['remote_create_user']. The value of $edit['remote_create_user'] is still false.

1

If you are actually trying to prevent a user from registering with an email that is in use, Drupal already has that built in. It also has a check that prevents users with an IP in the blocked IP address list to create an account.

Solution

Your flaw in the above code is that drupal_execute calls drupal_process_form which will rebuild the $form_state['values']. A solution could be to add a hidden/value form element to the user_register form with hook_form_alter. Doing that should allow you to send your custom value through drupal_execute.

|improve this answer|||||
  • I'm trying to verify it against an external database. – Matthew Hui Jun 4 '11 at 8:44
  • @glumbo Updated answer – googletorp Jun 4 '11 at 9:00
  • I tried it but still had a problem. Check the original question. – Matthew Hui Jun 5 '11 at 4:31

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.