'Illegal choice' error while submitting/validating selectbox shown with AJAX I'm making a module where the content type and fields are created by install. Now i want to make one of those fields only to show up when an option is selected. I did this with AJAX, in some way I can't make it work with the field as defined in the module.install file. I have a radio button selector for two options. When selecting the first option there should show up a selectbox with a list of nodes to select. When selecting the second option two textfields should be shown instead. When I try to add content of this type I get the error message (when changing the value from the radio buttons):

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

Can someone give me some advice to make this work? Thanks in advance!



Here is my code from the install file:

function _concert_installed_fields() {
$t = get_t();
return array(
  // other fields left out
'venue_node' =&gt; array(
 'field_name' =&gt; 'venue_node',
 'label' =&gt; 'Select a venue from the list',
 'type' =&gt; 'list_integer',
 'cardinality' =&gt; '1',
 'foreign keys' =&gt; array(),
 'indexes' =&gt; array(
   'value' =&gt; array(
    0 =&gt; 'value',
   ),
  ),
 'module' =&gt; 'list',
 'settings' =&gt; array(
  'allowed_values_function' =&gt; '_concert_content_type_venue_options_list',
  ),
),
'venue_radio' =&gt; array(
 'field_name' =&gt; 'venue_radio',
 'cardinality' =&gt; 1,
 'type' =&gt; 'list_boolean',
)
);


From my .module file:

 function concert_form_alter(&amp;$form, &amp;$form_state, $form_id) {
 if($form_id == "concert_node_form") {
 $form['venue_radio'] = array(
  '#title' =&gt; t("Choose a venue from the database or add one manually"),
  '#type' =&gt; 'radios',
  '#options' =&gt; array(0 =&gt; t('Add a venue from the database.'),
                      1 =&gt; t('Add a venue manually.'),),
  '#ajax' =&gt; array(
    'callback' =&gt; 'concert_venue_callback',
    'wrapper' =&gt; 'venue_select',
    'effect' =&gt; 'fade',
  ),
);

$form['venue_select'] = array(
  '#prefix' =&gt; '&lt;div id = "venue_select"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;',
  '#type' =&gt; 'fieldset',
);
if (!empty($form_state['values']['venue_radio']) &amp;&amp; $form_state['values']['venue_radio']   == "1") {
  $form['venue_node']['#type'] = 'hidden';

  $form['venue_select']['location'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('location'),

  );
  $form['venue_select']['venue_city'] = array(
    '#type' =&gt; 'textfield',
    '#title'=&gt; t('city'),
  );
}

else {
$form['venue_select']['venue_node'] = array(
    '#type' =&gt; 'select',
    '#options' =&gt; _concert_content_type_venue_options_list(),
    '#title' =&gt; t('venue node select'),
    '#default_value' =&gt; '466', // node id is select value
  );
if (!empty($form_state['values']['venue_node']))
$form['concert_venue_node']['und']['#default_value'] = $form_state['values']['venue_node']; 
}

function concert_venue_callback($form, $form_state) {
   return $form['venue_select'];
}


The options from the selectbox are populated by a function in a file accessable for both the .install and the .module file:

function _concert_content_type_venue_options_list() {

$results = db_select('node', 'n')
 -&gt;fields('n', array('nid', 'title'))
 -&gt;condition('n.status', 1)
 -&gt;condition('n.type', 'venue')
 -&gt;execute();

$options = array();
  foreach ($results as $node) {
    $options[$node-&gt;nid] = $node-&gt;title;
  }
return $options;
}
Setting #default_value for the 'venue_radio' form element solved my problem: http://drupal.org/node/1545316#comment-5914060
I also changed my code to how it is described in the Ajax altering node form example. Everything is working now in the way I wanted it to. Issue solved.