By dtcamtu on
hey, everybody
Can you help me?
In drupal, how to set attribute for radiobutton in group?
I have an array to create a group radio buttons:
$form['radio_group'] = array('#type' => 'radios',
'#title' => t('Radio'),
'#default_value' => t('1'),
'#options' => array('option1'=>t('option 1'),'option2'=>t('option 2'),'option3'=>t('option 3'))
);
I want to set individually atrribute for each item, for example, attribute of option1 is disable=true, and option2 and option3 not set.
Can I do that?
I'm very happy to receive your answers! Thanks!
Comments
use checkboxes instead of
use checkboxes instead of radio buttons or change your application logic (change conditions needed to take an action).
actually...
some people might need the functionality radio buttons offer. and just hiding options which are currently not available to the user is not the way to go either. one might want to disable the option and include a title attribute or perhaps even a note on the left explaining why it is disabled and what can be done to solve the issue.
i know there's a 'radio' form element type available with the form api, but there's no real explanation on how to use it in the docs. am i supposed to build an array of these and stick them in the '#options' attribute of the 'radios' element? anyone figure this out?
thanks!
You can disable radio buttons...
There is a way to set attributes for (or disable) individual radio buttons in Drupal 5, but it is not super straight forward. I've only used this method to disable individual radio buttons, so I'll stick with that as an example, but presumably you could set #attributes for each radio as well.
If you check out the form.inc file, you'll see that the '#options' key is just a shortcut for you. It actually creates individual radio types for each option. So the way you disable individual options is create your own radio types and no #options. Here's an example.
Say you wanted to disable the 'Optional' in this example from the forms api reference (please note that though there is no key given in the #options array, the keys are actually 0 & 1, though I've only done this with associative arrays):
Instead you would use the following:
A final couple of notes - If you don't set the '#parents' item correctly, your radios will not act like a radio group. And I don't know what #spawned does, but it is set for each item in the #options list, so I just stuck it in there with a hardcoded true.
Thanks
Thanks for the #parents explanation. I was going nuts over this. I needed radios in a table to provide info regarding each option and couldn't see how to do that if I was forced to pass the options in as an array.
Still works in D7
8 years later and this solution still works. hook_form_alter() is so inelegant for this type of thing and it lets me enable them later on without hunting down hook code. I hope this continues to work.
change attribute for radio specified radio button
For disabling specified checkbox you can use followed function in form_alter hook:
Or for disabling field this one:
simple solution to disabled individual form checkboxes
I have a list of items, each with a corresponding checkbox--pretty much like the admin content page.
I needed the ability to set the disabled property for individual checkboxes, depending on if a particular condition is true.
I explored several ways to accomplish this including patching form.inc, but I didn't want to hack the core.
The solution turned out to be quite simple. When outputting the form elements in a theme, simply add the disabled property to the attributes array before rendering the form element with drupal_render(). For example:
In this case $key is a single array from a form array in a foreach loop.
Disable radio button of webform
Hi, I have created a form using webfom.
Now I want one of the radio button in disabled mode. How can I do that?
I tried to achieve that using form_alter but I didn't get the solution which I need.
Thanks inadvance
you can do with javascript
you can do with javascript
chetan