You are here

drupal 字段集 fieldset(1)

g089h515r806 的头像
Submitted by g089h515r806 on 星期二, 2009-08-18 15:50

 

很多时候,你想将你的表单划分到不同的字段集中---使用表单API可以很容易的做到这一点。每一个字段集都定义在表单的数据结构中,而它所包含的字段都定义为它的孩子。让我们向我们的例子中添加一个“喜欢的颜色”字段:
 
function formexample_nameform() {
    $form['name'] = array(
       '#title' => t('Your Name'),
       '#type' => 'fieldset',
       '#description' => t('What people call you.')
    );
    $form['name']['user_name'] = array(
        '#title' => t('Your Name'),
        '#type' => 'textfield',
        '#description' => t('Please enter your name.')
    );
    $form['color'] = array(
       '#title' => t('Color'),
       '#type' => 'fieldset',
       '#description' => t('This fieldset contains the Color field.'),
       '#collapsible' => TRUE,
       '#collapsed' => FALSE
    );
    $form['color_options'] = array(
       '#type' => 'value',
       '#value' => array(t('red'), t('green'), t('blue'))
    );
    $form['color']['favorite_color'] = array(
       '#title' => t('Favorite Color'),
       '#type' => 'select',
       '#description' => t('Please select your favorite color.'),
       '#options' => $form['color_options']['#value']
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit')
    );
    return $form;
}
 
    表单的显示结果如图10-3所示:
10-3 带有字段集的简单表单
老葛的Drupal 培训班 Think in Drupal

Drupal版本: