I'm building quite a complicated and large custom form. Below I've made a simple example form to demonstrate my case:
function mymodule_form($form, &$form_state) {
$form['step1']['element1'] = array(
'#type' => 'select',
'#title' => 'Element 1',
'#options' => array('All' => '- Any -', 'option1' => 'Option 1', 'option2' => 'Option 2'),
);
$form['step1']['element2'] = array(
'#type' => 'select',
'#title' => 'Element 2',
'#options' => array('All' => '- Any -', 'option1' => 'Option 1', 'option2' => 'Option 2'),
);
$form['step1']['addfilter'] = array(
'#type' => 'submit',
'#value' => 'Add filter',
'#name' => 'addfilter',
'#ajax' => array(
'callback' => 'mymodule_form_callback',
'wrapper' => 'results-div',
),
);
$form['step2'] = array(
'#type' => 'fieldset',
'#title' => t('Results'),
'#prefix' => '<div id="results-div">',
'#suffix' => '</div>',
);
// Ajax values process
if(isset($form_state['values'])) {
foreach($form_state['values'] as $key => $value) {
if($value != 'All') {
$form['step2']['step2-'.$key] = array(
'#type' => 'button',
'#value' => 'remove ' . $key,
'#name' => 'step2-'.$key,
'#ajax' => array(
'callback' => 'mymodule_form_remove_callback',
'wrapper' => 'results-div',
),
);
}
}
}
}
function mymodule_form_callback($form, &$form_state) {
return $form['step2'];
}
function mymodule_form_remove_callback($form, &$form_state) {
// What to do here???
}
Basically what should happen is:
- Select a value from the step1 elements
- Click Add Filter button
- The form function runs again, and this time it creates ajax'd "remove" buttons under step2.
- When you press the remove button, the corresponding form element in step1 should get its value reset to
'All'
and the corresponding step2 button needs to get removed.
I'm getting stuck at step 4, I'm not sure how to reset a submitted multi-step form's values.