AJAX error: node form + list field with multiple options + image field I'm trying to add an AJAX callback to a list field with multiple selection enabled. This works properly on its own, but if I add an image field, the form will throw errors when 2 or more fields are selected.

Here's my setup:


Blank installation of Drupal 7 + image + list modules.
Create a "Sample" (machine name: sample) content type.
Add an image field ("Image").
Add a list field with possibility to select unlimited values ("List").
Enable the custom module below.
Try creating a new node and select 2 options in the "List" field. This will throw the following AJAX validation error:
"An illegal choice has been detected. Please contact the site administrator."


Code:

//
// File: sample.module
//
function sample_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  switch ($form_id) {
    case 'sample_node_form':
      $form['#id'] = $form_id;
      $form['field_list'][LANGUAGE_NONE]['#ajax'] = array(
        'method' =&gt; 'replaceWith',
        'wrapper' =&gt; $form_id,
        'callback' =&gt; 'sample_form_alter_ajax',
      );
      break;
  }
}
function sample_form_alter_ajax($form, $form_state) {
  // Of course, my logic will be added here.
  return $form;
}

//
// File: sample.info
//
name = Sample
description = Sample
package = Sample
core = 7.x
version = 7.x-1.0


Am I doing something wrong or is this a bug in image.module?
Is there anything I need to change in the way I set up my AJAX callback?

Thanks!

Update 1

The problem can be further narrowed to any file upload field. Here's a sample module that can be used standalone to replicate the issue. No need for creating content types.

sample.module file content:

function sample_menu() {
  $items['sample'] = array(
    'title' =&gt; 'Sample page',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('sample_page_form'),
    'access callback' =&gt; TRUE,
  );

  return $items;
}

function sample_page_form($form, &amp;$form_state) {
  $form['#id'] = 'sample-page-form';
  $form['list'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; t('Select something'),
    '#options' =&gt; array(
      'option1' =&gt; t('Option 1'),
      'option2' =&gt; t('Option 2'),
      'option3' =&gt; t('Option 3'),
    ),
    '#multiple' =&gt; TRUE,
    '#ajax' =&gt; array(
      'method' =&gt; 'replaceWith',
      'wrapper' =&gt; $form['#id'],
      'callback' =&gt; 'sample_page_form_ajax',
    ),
  );
  $form['image'] = array(
    '#type' =&gt; 'file',
    '#title' =&gt; t('Test fileupload field'),
  );

  $form['actions'] = array('#type' =&gt; 'actions');
  $form['actions']['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; 'Submit the form',
  );

  return $form;
}

function sample_page_form_ajax($form, $form_state) {
  return $form;
}


Update 2

This seems to work properly if the field type is changed to checkboxes instead. Not a real solution though, just a workaround.
After additional testing, this seems to be a bug in Drupal core that affects only the select form elements.

Workaround is to change the field type to checkboxes. Everything else can stay the same. Values are then passed properly and everything works OK even with multiple file upload fields added to the same form.

Also the validation and submit handlers work as expected in this case.

I created a ticket for Drupal core here.