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 !