复选框元素的示例如下。该元素的呈现版本如图10-13所示。
$options = array(
'poison' => t('Sprays deadly poison'),
'metal' => t('Can bite/claw through metal'),
'deadly' => t('Killed previous owner') );
$form['danger'] = array(
'#title' => t('Special conditions'),
'#type' => 'checkboxes',
'#description' => (t('Please note if any of these conditions apply to your
pet.')),
'#options' => $options,
'#weight' => 25,
);
图10-13 复选框元素示例图
在验证和提交函数中,通常使用array_filter()函数来获取复选框的键。例如,假如在图10-13中前两个复选框被选中了,那么$form_state['values']['danger']将包含以下内容:
array(
'poison' => 'poison',
'metal' => 'metal',
deadly' => 0,
)
运行array_filter($form_state['values']['danger'])将生成只包含复选框的键的数组:array('poison', 'metal')。
复选框元素的常用属性如下:#attributes, #default_value, #description, #options, #prefix, #required, #suffix, #title, #tree (默认为TRUE), 和#weight.注意#process属性默认设为expand_checkboxes() (参看 includes/form.inc)。
老葛的Drupal培训班 Think in Drupal