Drupal - #states可见选项不使用日期类型

时间:2011-06-21 17:43:27

标签: forms drupal date

我正在尝试为显示小时数的Drupal模块创建一个过滤表单。我试图获取一个日期字段,仅显示用户是否想要按其过滤。我在隐藏的代码中有一个文本字段。我不知道该怎么做。这是我的字段代码:

$form['filters']['start-do'] = array(
    '#type' => 'checkbox',
    '#title' => t('Filter by start date'),
);

$form['filters']['start'] = array(
    '#type' => 'date',
    '#title' => t('Start Date'),
    '#description' => t('Show hours that started after this date.'),
    '#states' => array(
        'invisible' => array(
            ':input[name="start-do"]' => array('checked' => FALSE) 
        )    
    )
);

1 个答案:

答案 0 :(得分:2)

您已经忘记了示例中的“容器”类型表单元素。

尝试这样的事情:

$form['filters']['start-do'] = array(
  '#type' => 'checkbox',
  '#title' => t('Filter by start date'),
);
$form['filters']['container'] = array(
  '#type' => 'container',
  '#states' => array(
    'invisible' => array(
      'input[name="start-do"]' => array('checked' => FALSE)
    )
  )
);
$form['filters']['container']['start'] = array(
  '#type' => 'date',
  '#title' => t('Start Date'),
  '#description' => t('Show hours that started after this date.'),
);

总是值得检查Forms API参考:http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#states