Ready to chuck my laptop out the window. I MUST be doing something wrong . . . or this is a bug in the Forms API.
Here's the problem.
It's somewhat straight forward: I try to set default values for a checkboxes field in a multistep form and the default values don't get applied.
This only happens when:
1) The checkboxes field in NOT in the first form presented (i.e. not switch case 1), AND (I think)
2) The checkboxes field is also introduced in that subsequent case.
Here's a complete sample testform module that exhibits the behavior (apologies for the longish, and, likely ugly, sample - newbie programmer):
function testform_form($form_values=NULL) {
$form['#multistep'] = TRUE;
$form['#redirect'] = FALSE;
if (isset($form_values['step'])) {
$current_step = ++$form_values['step'];
} else {
$current_step = 1;
}
$form['step'] = array(
'#type' => 'hidden',
'#value' => $current_step,
);
switch($current_step) {
case 1:
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#description' => t('test field');
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
break;
case 2:
$options = array('10' => t('Bobby'), '12' => t('Ralph'), '20' => t('George'));
$defaults['test'][] = '12';
$form['details'] = array (
'#type' => 'fieldset',
'#title' => t('Details'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['details']['admin'] = array (
'#type' => 'checkboxes',
'#title' => t('Only admin can view'),
'#default_value' => $defaults['test'],
'#options' => $options,
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 30,
'#maxlength' => 64,
'#description' => t('Enter the name for this group of settings'),
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Continue'));
break;
}
return $form;
}
function testform_page() {
return drupal_get_form('testform_form');
}
function testform_submit($form_id, $form_values) {
switch($form_values['step']) {
case 1:
break;
case 2:
break;
}
}
function testform_menu() {
global $user;
$items = array();
$items[] = array('path' => 'testform',
'title' => t('Test Form'),
'callback' => t('testform_page'),
'access' => user_access('access content') && ($user->uid > 0),
'type' => MENU_CALLBACK
);
return $items;
}
If I use this same method of setting the default values WITHOUT putting it in a multistep form . . . NO PROBLEM; it sets the values in what I believe is the correct fashion (cue: bang forehead against wall):
function testform_form($form_values=NULL) {
$options = array('10' => t('James'), '12' => t('Bobby'), '20' => t('George'));
$defaults['test'][] = '12';
$form['details'] = array (
'#type' => 'fieldset',
'#title' => t('Details'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['details']['admin'] = array (
'#type' => 'checkboxes',
'#title' => t('Only admin can view'),
'#default_value' => $defaults['test'],
'#options' => $options,
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Continue'));
return $form;
}
function testform_page() {
return drupal_get_form('testform_form');
}
function testform_submit($form_id, $form_values) {
}
function testform_menu() {
global $user;
$items = array();
$items[] = array('path' => 'testform',
'title' => t('Test Form'),
'callback' => t('testform_page'),
'access' => user_access('access content') && ($user->uid > 0),
'type' => MENU_CALLBACK
);
return $items;
}
Somebody please, please, please tell me where I've gone wrong. Save my laptop.