TO reproduce the error :
-> add an multi-values reference entity field to a content type
-> in a hook_form_alter, ajaxify the field

function mymodule_form_alter(&$form, &$form_state, $form_id) {
 if ($form_id == 'mycontenttype_node_form') {
    $form['#prefix'] = '<div id="test-form-wrapper">';
    $form['#suffix'] = '</div>';
    $form['field_test']['und']['#ajax'] = array(
        'callback' => 'mymodule_form_ajax_callback',
        'wrapper' => 'test-form-wrapper',
        'method' => 'replace',
        'event' => 'change',
        'effect' => 'fade',
    );
 }
}

-> Simply return the complete form

function mymodule_form_ajax_callback(&$form, &$form_state) {
   return $form;
}

If you select more than 1 value, you will have an illegal choice warning.
in the $form_state['values']['field_test'][0]['target_id'] you have the field selected keys separated by a comma instead of an array with multiple delta :
$form_state['values']['field_test'][0]['target_id'] = 12,45
Instead of
$form_state['values']['field_test'][0]['target_id'] = 12
$form_state['values']['field_test'][1]['target_id'] = 45

If you disable the return $form in the mymodule_form_ajax_callback, there is no warning.

Any idea ?

Comments

bneel’s picture

bneel’s picture

Issue summary: View changes
bneel’s picture

Ok, I found it. It is not related to entity reference.
This is a bug in Ajax component.

It appears every time you use an ajaxified select form type in a hook_form_alter .

See this post #5 https://drupal.org/comment/7927057#comment-7927057

bneel’s picture

Status: Active » Closed (fixed)