1
votes

I'm trying to use drupal_render() to render a single form element. I can successfully render elements of '#type' => 'textfield' or 'radio' or 'whatever'.

When I try to render an element of '#type' => 'radios' something goes wrong. I can't find out why but the radios simple won't show.

$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>
// "

Anyone knows what's the problem and the fix/workaround?
Is there any known problem with rendering radios or something?

Thanks!

5

5 Answers

3
votes

You can't render form elements without a form because the the radios element has a process callback of form_process_radios() that is called only when used with the form API.

You might be able to try something like:

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

For D7 use form_process_checkboxes()

0
votes

I was working on something similar an hour ago. Your code pasted into my form works just fine.

Try

drupal_get_form('your_form_id');

Does that work?

0
votes

I had the same trouble with the checkboxes element I wanted to render in a table. I found your answer a little bit to late.

So for all other searching how to render a checkboxes element with drupal_render and expand_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']));
0
votes

I might add, that in D6 you need to add #parents => array() to the radios element. if no, expand_radios will throw an error, in my case. Drupal 6.22