Search code examples
phpdrupalformsdrupal-6

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


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!


Solution

  • 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);