By cdesautels on
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
trying to work this out too!
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.
dupe
dupe
I had this same problem
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.
Doesn't play nice with #required
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
Working example in webform.module
I found a working example in the 5.x version of webform. It looks like the key is to explicitly specify the
#namefor each member of the radio group (equivalent to thenameattribute you'd use with<input type="radio"...>). You use unique entries in the$formarray, just as always (equivalent to theidattribute).The example in webform.module also explicitly specifies the
#parentsarray 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
Why #parents is specified
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
#parentsexplicitly, I received all of the values of the radio buttons, not just the selected one in the submit function.Example:
When I did not specify
#parents, the submit function recievedNow I get
array( 'a' => array('aa' => $keyX) )making it possible to actually decide which of the radio buttons was selected.
--
~/.singatrue: file not found