I have a form with 3 sequential selects at the top, each populated as a result of the selection in the one prior.
After the third select is choice is made, a large number of fields segregated into two vertical tabs should be populated.
- In the form, I set the default value of each of the fields to an associative array element, which is empty when the form is first built.
- When the third select choice is made, its callback a. obtains an associative array of the values needed for the form b. stores them in $form_state['storage']['mykey'] c. sets $form['rebuild'] to true (for good measure) d. returns $form
- In the form function, prior to the field definitions, I set the associative array used for the default values to $form_state['storage']['mykey']
I can see in xdebug that $form_state has the data in the correct place prior to returning, but when execution returns to the form function, $form_state no longer has a ['storage'] key.
AJAX CALLBACK
function my_ajax_callback($form, &$form_state) {
$form_state['storage']['draft'] = my_mysql_get($form_state['values']['id']);
return $form;
}
FIRST LINE OF FORM FUNCTION (most fields use $r for default_value)
$r = !isset($form_state['storage']['draft']) ? array() : $form_state['storage']['draft'];
A breakpoint at the callback reveals that it is reached, and that $form_state['storage']['draft'] is populated at the point of returning.
A breakpoint at the equate reveals that ['storage'] is no longer in $form_state, but this is only reached (in the form function) if I return only a field from the callback, not the entire form. I really need the entire form, because 17 of 20 fields on the form need to be rebuilt at that point, to get the default value into them.
FORM FIELD WITH AJAX CALLBACK
$form['info'] = array(
'#type' => 'select',
'#title' => 'Player',
'#prefix' => '<div id="info_select">',
'#suffix' => '</div>',
'#options' => info_options($form_state['values']['key1'], $form_state['values']['key2']),
// ajax to bind this select to the players select
'#ajax' => array(
'event' => 'change',
'callback' => 'my_ajax_callback',
'wrapper' => 'my_form_id',
),