So here is my problem:

I have a form so users can create orders. There is a 'Client' select list where they can select the client of the order. When we select a client, we use ajax to load client info (address, tax rate, discount ...) and populate some field. This works fine.

I am now adding a feature to be able to create directly a client from the order, in a modal. So I have a button in the order form, that triggers ajax to open a modal and render the new client form:

  $account_form = drupal_get_form('mymodule_creation_account_custom_form',$form['field_client']);
  $output = drupal_render($account_form);

  $commands = array();
  $commands[] = ajax_command_invoke('div#createClientModal','foundation',array('reveal', 'open'));
  $commands[] = ajax_command_replace('#new-account-form-placeholder',$output);
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,

Then I fill in the info, and submit the account from the modal:

  $account = my_module_new_account_form_submit($form, $form_state);

  $form['client_hidden_field']['und']['#options'][$account->nid] = $account->title;
  $form['client_hidden_field']['und']['#default_value'] = $form['client_hidden_field']['und']['#value'] = $account->nid;

  $commands = array();
  // Closing the modal
  $commands[] = ajax_command_invoke('div#createClientModal','foundation',array('reveal', 'close'));
  $commands[] = ajax_command_replace('.field-name-field-client', render($form['client_hidden_field']));
  $commands[] = ajax_command_invoke('.field-name-field-client select','change');

  // Replace the form with an empty div
  $commands[] = ajax_command_replace('div#account-form-wrapper','<div id="new-account-form-placeholder"></div>');

  return array(
    '#type' => 'ajax',
    '#commands' => $commands

So what I am doing in this last submit, is creating the new account (that works), it returns me the nid. Then, as I have stored in a variable ($form['client_hidden_field']) the client field, I update it with the new option, select the option, replace the old client select list with the new one, and trigger change to call the old callback that loads a client info when it is selected.

I have tried lots of thing and each time I select the new client created, I get an 'Illegal choice'. I have tried without default value, without triggering a 'change', in JS with a command_invoke and nothings seems to work.

The only success I had was : I do all these step, I manually select a client (one already existing before), and then select the new one. After that I didn't have the 'Illegal choice' and was able to save. But this is not a way to make it work.

If someone has any idea, I would love your help !



  • I think this is a good place to understand a bit better where I am wrong: drupal.stackexchange.com/questions/15989/… But because the second ajax callback is about the second $form in the modal, it returns me the new refreshed field, in the modal, not replacing the previous one!
    – Doud
    Commented Apr 29, 2015 at 16:32

2 Answers 2


Have you tried with $form_state['rebuild']=TRUE. It will help you to update your changes while adding new client.

  • Where would you add it? It should be when the modal close I guess, but at this point, my $form and $form_state are the one from new client form in the modal.
    – Doud
    Commented Apr 29, 2015 at 12:18
  • You should right it in hook_submit. your current submit hook hook as mention above. you code put it after this line:$form['client_hidden_field']['und']['#default_value'] = $form['client_hidden_field']['und']['#value'] = $account->nid;
    – Viraj
    Commented Apr 29, 2015 at 12:45

Ok, so I was finally able to succeed this. I didn't manage to return the new field to the form properly (cf. Resolve the error "An illegal choice has been detected...") because I am working with two forms (and maybe I am missing something).

So skipping the validation for that field in form.inc seems to be the only solution that I have. And the only way I found was to add a function in #after_build to unset '#needs_validation':

In form_alter:


And the function:

function remove_client_validation($form_element) {
  return $form_element;

Thanks to: I use jQuery to alter the exposed form select options. Got error: "An illegal choice has been detected." How to bypass validation?, where I found that solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.