How does one disable some (but not all) options in checkboxes or radios?
I need this in some form_alter hooks where the idea is to selectively disable one or more options in a checkboxes or radios item dependent upon the current user. The disabled options remain visible but cannot be selected.

Comments

mooffie’s picture

See http://drupal.org/node/244232 (which talks about '#attributes', but it's the same for '#disabled'.)

chaldar’s picture

Great! Thanks a lot.

ohnobinki’s picture

For drupal6.17, I tried this. However, I found that I needed to set ['#attributes']['disabled'] instead of just ['#disabled'] to get any change.

My example #after_build function:


function ccelmod_memberships_form_disable_types( $form_element, &$form_state )
{
	foreach( array_keys( $form_element ) as $membership_type )
		if( substr( $membership_type, 0, 1 ) != '#' )
		{
			$form_element[$membership_type]['#disabled'] = !db_result( db_query( 'SELECT active FROM {ccel.membership_types} WHERE membership_type=%d', $membership_type ) );
			/* see include/form.inc:_form_builder_handle_input_element() */
			if($form_element[$membership_type]['#disabled'])
				$form_element[$membership_type]['#attributes']['disabled'] = NULL;
		}
	return $form_element;
}
mooffie’s picture

D6 has a bug with disabled fields. Unless your checkboxes are all off, you probably want to not disable them (Or just try it and see if it works; some fields work correctly nevertheless).