【问题标题】:Adding multiple checkboxes in Drupal-form在 Drupal 表单中添加多个复选框
【发布时间】:2011-12-22 10:07:30
【问题描述】:

我想在我的 D7 表单中添加一些复选框。出于某种原因,下面的 sn-p 不起作用。任何想法为什么或任何建议如何正确地做到这一点?

$options = array('A', 'B', 'C');
foreach ($themas as $thema) {

        // Initialize array
        $ra = array();

        // Fill up the array with different keys
        $key = $prefix.'_thema_'.$thema->tid.'_fiche';
        $ra[$key]['#type'] = 'checkboxes';
        $ra[$key]['#name'] = $prefix.'_thema_'.$thema->tid.'_opties';
        $ra[$key]['#options'] = $options;
}

【问题讨论】:

    标签: php forms drupal checkbox drupal-7


    【解决方案1】:

    我认为这是因为您在循环的每一步都重新初始化 $ra,所以它只会包含一组复选框。尝试在循环之外初始化它:

    $options = array('A', 'B', 'C');
    
    // Initialize array
    $ra = array();
    
    foreach ($themas as $thema) {
        // Fill up the array with different keys
        $key = $prefix.'_thema_'.$thema->tid.'_fiche';
        $ra[$key]['#type'] = 'checkboxes';
        $ra[$key]['#name'] = $prefix.'_thema_'.$thema->tid.'_opties';
        $ra[$key]['#options'] = $options;
    }
    
    $form['some_key'] = $ra;
    

    还要确保您的 $prefix 字符串不以 # 符号开头,否则 Drupal 会将其视为属性而不是需要呈现的元素。

    【讨论】:

    • 确实是初始化问题! #options 的值每次都会被覆盖。谢谢
    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    相关资源
    最近更新 更多