Ajax not working with existing field in hook_form_alter If I add new select field and define ajax, all works fine. Whereas if I attach #ajax to the existing select field it just does not work. (I can see ajax loader spining for a while and that is all.) I'm also getting error: "An illegal choice has been detected. Please contact the site administrator."

I'm suspecting my callback function is not correctly implemented. Is it valid to make changes to the form in the callback function?

function hook_form_alter(&amp;$form, &amp;$form_state, $form_id) {
    //...

        // add wrapper to existing select field
        $form['field_event_1_role']['und']['#prefix'] = '&lt;div id="select-event-role"&gt;';
        $form['field_event_1_role']['und']['#suffix'] = '&lt;/div&gt;';
        $form['field_event_1_role']['und']['#options'] = array('_none' =&gt; t(' - '));

        $events_list_options = array('1' =&gt; t('1'), '2' =&gt; t('2'));

        // **add new select field with ajax - This is working fine**
        $form['test_select_ajax'] = array(
            '#title' =&gt; t('Test select ajax'),
            '#type' =&gt; 'select',
            '#options' =&gt; $events_list_options,
            '#ajax' =&gt; array(
              'event'=&gt;'change',
              'callback' =&gt;'event_roles_ajax_callback',
              'wrapper' =&gt; 'select-event-role',
            ),
        );

        // **attach ajax to the existing select field - NOT working?!**
        $form['field_event_1']['und']['#options'] = $events_list_options;
        $form['field_event_1']['und']['#ajax'] = array( 
            'event'=&gt;'change',
            'callback' =&gt;'event_roles_ajax_callback',
            'wrapper' =&gt; 'select-event-role',
        );                  
    }

    // Ajax callback function
    function event_roles_ajax_callback($form, &amp;$form_state) {
        $form['field_event_1_role']['und']['#options'] = array('populated' =&gt; t('populated'));
        return $form['field_event_1_role'];
    }


Additional info: The form I'm altering here is registration_form from Entity Registration module
for this you can delete this line 

$form['field_event_1_role']['und']['#options'] = array('_none' =&gt; t(' - '));


The problem is most likely because of a requirement for FAPI ajax which is documented, though can be easily missed: 


  Changes to the form must only be made in the form builder function (ajax_example_autocheckboxes() in the example here), or validation will fail. The callback function must not alter the form or any other state.


From AJAX Forms in Drupal 7.

The fact that you're setting the form element [#options] in the ajax callback is definitely a problem. The ajax callback should only return the array or HTML to be printed, not change the form. Only the return line should be in that callback. 

The changes to your form should be made in the form_alter. Also, you should use the form_state array to check whether a value has been selected for your first dropdown. This will be updated during an AJAX request.

I recommend looking at the examples module, as well as these pages for more information on AJAX in FAPI. It can definitely be tricky.


AJAX Forms in Drupal 7
AJAX Framework