Try changing your code to the following, no need for the call to form_process_checkboxes.
function my_module_block_view($delta='') {
$block = array();
switch ($delta) {
case 'myform':
$block['content'] = drupal_get_form('mymodule_form');
break;
}
return $block;
}
//下面这个未发现起作用
function mymodule_form() {
$form = array();
$form['tests_taken'] = array(
'#type' => 'checkboxes',
'#options' => drupal_map_assoc(array(t('SAT'), t('ACT'))),
'#title' => t('What standardized tests did you take?'),
'#attributes' => array(
'name' => 'example-checkboxes[]'
)
);
dsm($form);
return $form;
}
来自 http://drupal.stackexchange.com/questions/65011/accessible-checkboxes-using-drupals-form-api
You can use the new Form API feature, #states
to achieve this.
Note that, for ease of use, I'm wrapping the conditional checkboxes with a fieldset
.
<?php
$form['categories']['templates']['parent1'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the appropriate actions for the form based on the indicated template'),
'#options' => array(1 => 'Parent#1'), // Note this! You can't use 0 as a key in #options.
'#multiple' => TRUE,
);
$form['categories']['templates']['parent1']['wrapper'] = array(
'#type' => 'fieldset',
'#title' => t('Options for @option', array('@option' => 'Parent#1')),
'#states' => array(
'visible' => array(
':input[name="parent1[1]"]' => array('checked' => TRUE), // We use "parent1" and its Nth option. in above field, See #options in above checkboxes field.
),
),
);
$form['categories']['templates']['parent1']['wrapper']['actions'] = array(
'#type' => 'checkboxes',
'#options' => array("P1 - option#1", "P1 - option#2"), // This is also wrong. You must define a key => value pair for #options or somehow avoid using 0 as the key.
'#multiple' => TRUE,
);
?>
You might want to use #tree
=> TRUE in the fieldsets to avoid Drupal from merging same keys' values together.
Also, you won't need #multiple
in checkboxes field type.
Update
Example with node types:
<?php
$node_types = node_type_get_names();
$form['categories']['templates']['parent1'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the appropriate actions for the form based on the indicated template'),
'#options' => $node_types,
);
foreach ($node_types as $type => $label) {
$form['categories']['templates']['node-options'.$type] = array(
'#type' => 'checkboxes',
'#title' => t('Options for @type', array('@type' => $label)),
'#options' => array("Parent#1"),
'#multiple' => TRUE,
'#states' => array(
'visible' => array(
':input[name="parent1['.$type.']"]' => array('checked' => TRUE), // We use "parent1" and its Nth option. in above field, See #options in above checkboxes field.
),
),
);
}
来自 http://stackoverflow.com/questions/14878783/drupal-form-api-checkboxes-hierarchy
When you use #options
on a FAPI element the value passed to the $form_state
is the array key, so you don't need to use array_keys()
.
I'm not sure why you're using checkboxes
for a yes/no, usually one would use a simple checkbox
element. However if that's really what you want to do:
Your
#options
can't contain on option with0
as the array key, it will be automatically filtered out and you'll never know if that option has been checked.You should use
$heating_options_chosen = array_filter($form_state['values']['heating']
to get the selected checkbox options.
I honestly think your code should look like this though:
$form['checklist_fieldset']['heating'] = array(
'#type' => 'checkbox',
'#title' => t('Heating options'),
'#options' => array(
'1' => t('Yes'),
'0' => t('No')
),
'#description' => t('Heating details.')
);
$heating_checked = $form_state['values']['heating'] == 1;
来自 http://stackoverflow.com/questions/9955127/cannot-get-checkboxes-value-using-drupal-form-api