How to avoid "An illegal choice has been detected" when updating select list values via ajax? I have four entity reference fields to user entity in a form. Host form is node edit form. 
Three of them are select lists, last is Inline Entity Form.

After adding a user entity via IEF I'm trying to update select lists values:

function ebep_users_feature_users_list_ajax($form, $form_state) {

  $ief = array_shift($form_state['inline_entity_form']);
  $options = array('_node' =&gt; t('- None -'));
  $commands = array();

  foreach($ief['entities'] as $data) {
    $options[$data['entity']-&gt;uid] = $data['entity']-&gt;name;
  }

  foreach(_ebep_users_feature_getUserListFieldNames() as $fieldName) {
    $form[$fieldName]['und']['#options'] = $options;
    $commands[] = ajax_command_replace('#'.$fieldName.'_wrapper', drupal_render($form[$fieldName]));
  }

  return array('#type' =&gt; 'ajax', '#commands' =&gt; $commands);
}


It works, but when I try to add another user via IEF I am getting:


  An illegal choice has been detected. Please contact the site
  administrator.
--- Original answer. Is wrong, check update below.

Just set 

$bla['#validated'] = TRUE


On the select that will be updated with AJAX.

Checkout &quot;Illegal Choice has been detected&quot;

--- Update 17 Dec 2019 ---

Actually that was not good advice, that was a long time ago, now I know better.

Ajax callback function is supposed to only return the piece of form that has changed or some ajax commands.

In this case form modification must be made on the form function or a form alter hook. Form building and validation functions are run each time an ajax callback is called. Just check $form_state on your form build or alter function and adjust values accordingly.

See Resolve the error &quot;An illegal choice has been detected...&quot;