OK I know this question has been asked multiple times from people in different ways but I haven't been able to pinpoint my exact use case or solution.
I have a registration form that I want to prefill the default values after the user selects an option from a select list. After they select an option, AJAX fires off to see if the user is in our CRM, and runs a series of code to look up information. I then want that found information to show in the various form fields on the page.
Couple key things to point out that I often find in other solutions that do not work for me:
- The user is NOT submitting. They are still doing work on the form, the first form field sets up their experience going forward.
- Setting the field info in $form_state['values'] doesn't work. Nothing is set.
- Settings the field info $form_state['input'] does work, but then if a user changes any of the values in the fields, that new change isn't set.
Here is a stripped down example of what is being done.
function ces_user_registration_form_commerce_checkout_form_registration_alter(&$form, &$form_state, $form_id) {
foreach ($form['registration_information']['register_entities'] as $name => $data) {
foreach($form['registration_information'][$name] as $subname => $subdata) {
$form['registration_information'][$name][$subname]['register_for']['#ajax'] = array(
'wrapper' => 'commerce-checkout-form-registration',
'callback' => 'ces_user_registration_form_register_for_ajax_callback',
'method' => 'replace'
);
// run our code for comparing user values with CRM
// return object with CRM data
// below are example static value settings that I have
// This works after AJAX is called, but the field isn't visually filled in for the user,
$form['registration_information'][$name][$subname]['field_first_name']['und'][0]['value']['#default_value'] = "travis";
// This works after AJAX is called, but changes to the field are not saved or recognized so we save their old value
$form_state['input']['registration_information'][$name][$subname]['field_first_name']['und'][0]['value'] = "travis";
}
}
}
and the callback
function ces_user_registration_form_register_for_ajax_callback($form, &$form_state) {
foreach ($form['registration_information']['register_entities'] as $name => $data) {
foreach($form['registration_information'][$name] as $subname => $subdata) {
if(!empty($form_state['values']['registration_information'][$name][$subname]['register_for']) && $form_state['values']['registration_information'][$name][$subname]['register_for'] == 'registration_registrant_type_me' ){
// I tried moving the CRM code down here instead, but no changes
// run our code for comparing user values with CRM
// return object with CRM data
// below are example static value settings that I have tried
// doesn't work
$form['registration_information'][$name][$subname]['field_first_name']['und'][0]['value']['#default_value'] = "travis";
// doesn't work
$form_state['values']['registration_information'][$name][$subname]['field_first_name']['und'][0]['value'] = "travis";
// doesn't work
$form_state['input']['registration_information'][$name][$subname]['field_first_name']['und'][0]['value'] = "travis";
}
}
}
return $form
}