【问题标题】:Why drupal_render($form) does not render correctly RADIOS #type?为什么 drupal_render($form) 不能正确渲染 RADIOS #type?
【发布时间】:2011-02-02 10:37:22
【问题描述】:

我正在尝试使用 drupal_render() 来呈现单个表单元素。我可以成功渲染'#type' => 'textfield' or 'radio' or 'whatever' 的元素。

当我尝试渲染 '#type' => 'radios' 的元素时出现问题。我不知道为什么,但收音机不会显示。

$options = array(
    '0' => 'no option',
    '1' => 'option 1',
    '2' => 'option 2',
    '3' => 'option 3',
    '4' => 'option 4',
    '5' => 'option 5'
);

$form['radiosinput'] = array(
    '#type'             => 'radios',
    '#title'            => 'radios title',
    '#description'      => 'radios description',
    '#default_value'    => 0,
    '#options'          => $options,
    '#required'         => TRUE,
);

var_dump( drupal_render($form) );

// string(257) "<div class="form-item">
//     <label>radios title: <span class="form-required" title="This field is required.">*</span></label>
//     <div class="form-radios"></div>
//     <div class="description">radios description</div>
//     </div>
// "

有人知道问题出在哪里以及修复/解​​决方法吗?
渲染收音机有什么已知问题吗?

谢谢!

【问题讨论】:

    标签: php drupal forms drupal-6


    【解决方案1】:

    您无法在没有表单的情况下呈现表单元素,因为 radios 元素具有 form_process_radios() 的进程回调,该回调仅在与表单 API 一起使用时调用。

    您也许可以尝试以下方法:

    $form['radiosinput'] = expand_radios($form['radiosinput']);
    return drupal_render($form);
    

    【讨论】:

    • form_process_* 是特定于 D7 的。我正在寻找 D6 解决方案。还是谢谢。
    • 啊,那就用$form['radiosinput'] = expand_radios($form['radiosinput']);
    • 请用该信息更新您的答案,我会接受! :) drupal_render(expand_radios($form['radiosinput'])); 完成了这项工作。谢谢。
    【解决方案2】:

    对于 D7,使用 form_process_checkboxes()

    【讨论】:

      【解决方案3】:

      一个小时前我正在做类似的事情。您粘贴到我的表单中的代码可以正常工作。

      试试

      drupal_get_form('your_form_id');

      这行得通吗?

      【讨论】:

      • 谢谢,但我没有呈现表单,只是呈现特定元素...如果我将代码包含在表单中,它也可以正常工作。
      • @andre matos 了解为什么要在没有表单的情况下呈现单选按钮可能会有所帮助。
      • 你可以通过主题层来运行表单元素吗? theme_radios() 什么的……
      【解决方案4】:

      我想在表格中呈现的复选框元素遇到了同样的问题。我发现你的答案有点晚了。

      所以对于所有其他搜索如何使用drupal_renderexpand_checkboxes 呈现复选框元素:

      $form['test'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Test'),
        '#description' => t('The description appears usually below the checkboxes.'),
        '#options' => array(1,2,3,4),
      );
      
      drupal_render(expand_checkboxes($form['test']));
      

      【讨论】:

      • 在 Drupal 7 中相当于什么?
      【解决方案5】:

      我可能会补充一点,在 D6 中,您需要将 #parents => array() 添加到 radios 元素。 如果不是,在我的情况下,expand_radios 会抛出一个错误。 Drupal 6.22

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-06
        • 1970-01-01
        • 1970-01-01
        • 2022-06-15
        • 2021-05-28
        • 2018-04-14
        • 2020-07-11
        • 1970-01-01
        相关资源
        最近更新 更多