"An illegal choice has been detected" error on user creation When creating a new user, I got this error:


  An illegal choice has been detected. Please contact the site administrator.


I'm sure I entered all the required information. This is when I am logged-in as administrator.
I'm using drupal 7.10.

[Edit]
I have a few lines of code in my hook_form_alter() which alters the user registration form :  

if($form_id == 'user_register_form'){
    $form['profile_main']['#weight'] = 10; 
    # Password and Confirm password
    $form['account']['pass']['#weight'] = -9; # move above E-mail address
    # User role
    $form['autoassignrole_user']['#weight'] = 2;
    $form['autoassignrole_user']['user_roles']['#attributes'] = array('onclick' =&gt; "Drupal.mymodule.toggleProfile(this)");
    if(in_array('administrator', $user-&gt;roles)){ # if master administrator, hide it
        $form['autoassignrole_user']['#required'] = FALSE;
        $form['autoassignrole_user']['#type'] = 'hidden';           
    }
}
elseif($form_id == 'user_profile_form'){
    if(!in_array('administrator', $user-&gt;roles)){ # if not administrator
        $elems[] = 'timezone';
        $form['timezone']['#required'] = FALSE;
        $form['timezone']['#type'] = 'hidden';          
    }
}
When you submit a form Drupal checks the integrity of it, i.e. it checks that the submitted values fall in line with the list of possible values for each element in the original form definition.

So, if you've got a select/checkboxes/radio buttons element on the form and you change the possible options for this using javascript on the client side, the new options won't match up with the ones Drupal knew about when the form was built. In this instance Drupal will rightly inform the user "An illegal choice has been detected.", because it has. This is an excellent security feature and is the default behaviour. It can't be switched off without some serious effort.

I would take a guess in your case that this problem is happening in the JS you're attaching to the autoassignrole_user element.

If you want to change a form's structure using JS you need to do it the 'Drupal' way, using AJAX to update the form on the server-side:

function MYMODULE_my_form($form, &amp;$form_state) {
  // If the checkbox is ticked, load one set of options
  if (isset($form_state['values']['check']) &amp;&amp; $form_state['values']['check']) {
    $options = new_options();
  }
  else {
    // Otherwise load the default options
    $options = default_options();
  }

  $form['check'] = array(
    '#type' =&gt; 'checkbox',
    '#title' =&gt; 'Click to change options',
    '#default_value' =&gt; isset($form_state['values']['check']) ? $form_state['values']['check'] : 1,
    '#ajax' =&gt; array(
      'wrapper' =&gt; 'options-wrapper',
      'callback' =&gt; 'my_form_ajax_callback'
    )
  );

  $form['options'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; 'Options',
    '#options' =&gt; $options,
    '#prefix' =&gt; '&lt;div id="options-wrapper"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;'
  );
}

function my_form_ajax_callback(&amp;$form, &amp;$form_state) {
  // Return the options element which will have been changed in the re-build of the form
  return $form['options'];
}


The above is a very simple AJAX example that uses a checkbox change event to change the values in a select list on the form. Doing it this way will keep the internal structure of the form intact and you will no longer get the error message you're currently getting.

Have a look at the Form API documentation and AJAX framework documentation for a lot more information on this.