I'm want to create a set of radio buttons, but the buttons need to be spread throughout the page. Does anyone know how to do this? To be tied together, they all have to have the same name of course. I can achieve this with the '#radios' element type, but then they all end up grouped together.

If I use the '#radio' type, I can put them any where on the page, but the element names collide and I end up with just one radio button with the 1st defined location and the last defined value.

Any insights would be great.

Comments

rkendall’s picture

it is a bit limiting that radio options can't be split up easily to allow sub-items to be inserted.

What I am trying to do i can imagine is quite a common use case, but the Forms API (v5) doesn't seem to have taken it into account.

Example:

o Radio option 1.
- Select box specific to radio option 1

o Radio option 2.
- Select box specific to radio option 2

Just looking for the least-hacky solution.

rkendall’s picture

dupe

damiancugley’s picture

My workaround in a similar situation: if all the radio items go in separate fieldsets, then they can have the same name (which is required for the browser to treat the radio buttons as mutually exclusive).

They don't even need to be separate fieldsets, just different locations in the tree structure of the form should do it.

HorsePunchKid’s picture

I've got mine in separate fieldsets, too, but it doesn't seem to work properly if you make the radio button required. When you do that, Drupal wants every radio button to be selected. I might be able to get around this by fiddling with the validation function. If I get it working, I'll follow up here.

-- Steven N. Severinghaus

HorsePunchKid’s picture

I found a working example in the 5.x version of webform. It looks like the key is to explicitly specify the #name for each member of the radio group (equivalent to the name attribute you'd use with <input type="radio"...>). You use unique entries in the $form array, just as always (equivalent to the id attribute).

The example in webform.module also explicitly specifies the #parents array for each radio. It's not clear to me that this is actually necessary, since it so far seems to work without it.

-- Steven N. Severinghaus

traxer’s picture

In case you're looking: it's in Webform 5.x-1.10; it seemingly has been dropped somewhere on the road to 5.x-2.1.3.

As long as I did not specify #parents explicitly, I received all of the values of the radio buttons, not just the selected one in the submit function.

Example:

$form = array(
  'a' => array(
    '#tree' => true,
    'aa' => array(
      '#type' => 'fieldset',
    ),
  ),
);

foreach ($array as $key => $value) {
  $form['a']['aa'][$key] = array(
    '#type' => 'radio',
    '#return_value' => $key,
    '#name' => 'a[aa]',
    '#parents' => array('a'),
);

When I did not specify #parents, the submit function recieved

array(
  'a' => array(
    'aa' => array(
      $key1 => substr($key1, 0, 1),
      $key2 => substr($key2, 0, 1),
      // and so forth for any other entry in $array
    ),
  ),
)

Now I get

array( 'a' => array('aa' => $keyX) )

making it possible to actually decide which of the radio buttons was selected.

--
~/.singatrue: file not found