簡體   English   中英

為什么drupal_render($ form)無法正確呈現RADIOS #type?

[英]Why drupal_render($form) does not render correctly RADIOS #type?

我正在嘗試使用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>
// "

任何人都知道有什么問題以及解決方法/解決方法嗎?
渲染收音機或其他東西有什么已知問題嗎?

謝謝!

對於D7,請使用form_process_checkboxes()

您不能在沒有表單的情況下呈現表單元素,因為radios元素具有form_process_radios()的進程回調,僅當與表單API一起使用時才調用該回調。

您也許可以嘗試類似的方法:

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

一個小時前,我正在做類似的事情。 將您的代碼粘貼到我的表單中就可以了。

嘗試

drupal_get_form('your_form_id');

那樣有用嗎?

我想要在表中呈現的復選框元素也遇到了同樣的麻煩。 我發現你的答案有點遲了。

因此,對於所有其他搜索,如何使用drupal_renderexpand_checkboxes呈現一個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']));

我可能要補充一點,在D6中,您需要向radios元素添加#parents => array()。 如果沒有,就我而言,expand_radios將引發錯誤。 Drupal 6.22

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM