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

iwankgb’s picture

use checkboxes instead of radio buttons or change your application logic (change conditions needed to take an action).

rhyek’s picture

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!

scafmac’s picture

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):

 $form['posting_settings']['comment_preview'] = array(
  '#type' => 'radios',
  '#title' => t('Preview comment'),
  '#default_value' => variable_get('comment_preview', 1),
  '#options' => array(t('Optional'), t('Required')),
);

Instead you would use the following:

 $form['posting_settings']['comment_preview'] = array(
  '#type' => 'radios',
  '#title' => t('Preview comment'),
  0 => array(
    '#type' => 'radio',
    '#title' => t('Optional'),
    '#return_value' => 0,
    '#default_value' => variable_get('comment_preview', 1),
    '#parents' => array('comment_preview'),
    '#spawned' => TRUE,
    '#disabled' => TRUE,
  ),
  1 => array(
    '#type' => 'radio',
    '#title' => t('Required'),
    '#return_value' => 1,
    '#default_value' => variable_get('comment_preview', 1),
    '#parents' => array('comment_preview'),
    '#spawned' => TRUE,
  ),
);

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.

regx@drupal.org’s picture

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.

zkent’s picture

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.

kenorb’s picture

For disabling specified checkbox you can use followed function in form_alter hook:

/*
 * Change options for individual checkbox or radio field in the form
 * You can use this function using form_alter hook.
 * i.e. _set_checkbox_option('field_tier_level', 'associate', array('#disabled' => 'disabled'), $form);
 *
 * @param $field_name (string)
 *    Name of the field in the form
 * @param $checkbox_name (string)
 *    Name of checkbox to change options (if it's null, set to all)
 * @param $options (array)
 *    Custom options to set
 * @param $form (array)
 *    Form to change
 *
 * @author kenorb at gmail
 */
function _set_checkbox_option($field_name, $checkbox_name = NULL, $options, &$form) {
    if (isset($form[$field_name]) && is_array($form[$field_name])) {
        foreach ($form[$field_name] as $key => $value) {
            if (isset($form[$field_name][$key]['#type'])) {
                $curr_arr = &$form[$field_name][$key]; // set array as current
                $type = $form[$field_name][$key]['#type'];
                break;
            }
        }
        if (isset($curr_arr) && is_array($curr_arr['#default_value'])) {
            switch ($type) { // changed type from plural to singular
                case 'radios':
                    $type = 'radio';
                    break;
                case 'checkboxes':
                    $type = 'checkbox';
                    break;
            }

            foreach ($curr_arr['#default_value'] as $key => $value) {
                foreach($curr_arr as $old_key => $old_value) { // copy existing options for to current option
                    $new_options[$old_key] = $old_value;
                }
                $new_options['#type'] = $type;  // set type
                $new_options['#title'] = $value;  // set correct title of option
                $curr_arr[$key] = $new_options; // set new options

                if (empty($checkbox_name) || strcasecmp($checkbox_name, $value) == 0) { // check name or set for 
                    foreach($options as $new_key => $new_value) {
                        $curr_arr[$key][$new_key] = $value;
                    }
                }
            }
            unset($curr_arr['#options']); // delete old options settings
        } else {
            return NULL;
        }
    } else {
        return NULL;
    }
}

Or for disabling field this one:

/*
 * Disable selected field in the form(whatever if it's textfield, checkbox or radio)
 * You can use this function using form_alter hook.
 * i.e. _disable_field('title', $form);
 *
 * @param $field_name (string)
 *    Name of the field in the form
 * @param $form (array)
 *    Form to change
 *
 * @author kenorb at gmail
 */
function _disable_field($field_name, &$form) {
    $keyname = '#disabled';

    if (!isset($form[$field_name])) { // case: if field doesn't exists, put keyname in the main array
        $form[$keyname] = TRUE;
    } else if (!isset($form[$field_name]['#type']) && is_array($form[$field_name])) { // case: if type not exist, find type from inside of array
        foreach ($form[$field_name] as $key => $value) {
            if (isset($form[$field_name][$key]['#type'])) {
                $curr_arr = &$form[$field_name][$key]; // set array as current
                break;
            }
        }
    } else {
        $curr_arr = &$form[$field_name]; // set field array as current
    }

    // set the value
    if (isset($curr_arr['#type'])) {
        switch ($curr_arr['#type']) {
            case 'textfield':
            default:
                $curr_arr[$keyname] = TRUE;
        }
    }
}
johnhanley’s picture

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:

$form['occurrences'][$key]['#attributes'] = array('disabled' => 'disabled');
drupal_render($form['occurrences'][$key])

In this case $key is a single array from a form array in a foreach loop.

cnm1990’s picture

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

chetan-singhal’s picture

you can do with javascript

chetan