I have some elements in a form as below:
$form['provider'] = array(
'#type' => 'select',
'#title' => t('Provider'),
'#required' => TRUE,
'#options' => $some_options,
'#ajax' => array(
'callback' => 'my_callback_func',
'wrapper' => 'tests-div',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['tests'] = array(
'#type' => 'select',
'#title' => t('Tests'),
'#required' => TRUE,
'#prefix' => '<div id="tests-div">',
'#suffix' => '</div>',
'#options' => array(''),
'#validated' => TRUE,
);
When the provider
select box value is changed, the callback my_callback_func
checks the chosen value of the select box, and returns a modified version of tests
select box with some new options set.
function my_callback_func($form, &$form_state) {
[snipped code which reads form data and calculates $some_new_opts ]
$form['tests'] = array(
'#type' => 'select',
'#title' => t('Tests'),
'#required' => TRUE,
'#prefix' => '<div id="tests-div">',
'#suffix' => '</div>',
'#options' => $some_new_opts,
'#validated' => TRUE,
);
return $form['tests'];
}
So far so good - when I change the value of the provider
select I can see the Ajax event firing, the new options appear in the tests
checkbox and I can select from them. But then when I submit the form using
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'go for it',
'#submit' => array('my_post_submit_function'),
);
none of the Ajax supplied values are visible in either form
or form_state
within my_post_submit_function
. Instead, the original value (the empty array) is always shown in $form['tests']['#options']
, and $form_state['values']['tests']
is null ("").
What am I doing wrong ?
Updated 4th Sept
I'm still trying to solve this problem. I've looked in a debugger at what happens when I click the submit button.
index.php
and menu.inc
direct control to form.inc
, where drupal_get_form
makes a call to drupal_build_form
.
At this point $form_state
seems to be being reconstructed from somewhere (the cache_form
table?) as it certainly doesn't contain the settings I made to it during my earlier processing. (for e.g. I set $form_state['foo'] = "bar";
which has now disappeared.)
Does anybody know how this works in more detail? Does form caching mean that it's impossible to submit Ajax-modified data to the submit handler?