The attached patch allows to disable one or more items in a group of checkboxes or radios. You just need to enter the #disabled as array and not as boolean value, like this:
$options = array('1' => 'First item', '2' => 'Second item', '3' => 'Third item', '4' => 'Fourth item');
$disabled = array(2, 3);
$form['checkboxes'] = array('#type' => 'checkboxes', '#options' => $options, '#disabled' => $disabled);
In this example the checkboxes with keys 2 and 3 will be disabled. The #disabled array must contain the keys of the items that must be disabled. When #disabled is boolean, it works as usual and all items will be disabled.
Comments
Comment #1
ximo commented+1 for this patch, I think the Forms API should include this
Comment #2
ximo commentedCould the Forms API for checkboxes and radios be rewritten to support the following code?
In other words, have Forms API support the #checkbox/#radio type for every checkbox/radio option, giving each option the same properties as a stand-alone checkbox/radio. This would allow for more advanced forms, and would make this suggestion easy to implement. However it looks like a lot of rewrite, and unfortunately I don't know form.inc enough to implement this myself (yet).
Comment #3
ximo commentedHere's a an updated version of stafano73's patch for revision 1.177..
Comment #4
shunting commentedUntil the forms API does include it, you can set the #attributes property of the individual form element (button, box, etc) to something like array('disabled'=>'disabled') if you want.
See
http://api.drupal.org/api/HEAD/file/developer/topics/forms_api_reference...
Less than elegant but it does work.
Comment #5
tonyn commented+1, looking forward to this.
Comment #6
junyor commentedIt would be good to be able to disable individual options in a SELECT element, too.
Comment #7
scafmac commentedIs a patch really needed? You can control individual radio buttons with the 5.x forms api - http://drupal.org/node/68740#comment-268897 - you just cannot use the '#options' item...
Comment #8
junyor commentedYour approach is rather difficult and hidden compared to just having #options. This should apply to radio buttons, check boxes, options, and optgroups.
Comment #9
chx commentedPatch needs reroll and despite this is a cool feature, we are not adding new features, small or big to core now.
Comment #10
panchoHere's a rerolled patch against HEAD (which is now 7.x-dev). Please test.
Comment #11
panchoRemark: This one here is blocking #119038
Comment #12
kenorb commentedFor disabling specified checkbox you can use followed function in form_alter hook:
Or for disabling field this one:
Comment #13
lilou commentedPatch no longer applied.
Comment #14
jeffschuler+1 for this functionality.
Re-rolled Pancho's #10 to apply to HEAD.
Comment #15
catchThis looks good - seems like we could use it for the forum select to exclude containers? That'd make it easy to write a test for this as well.
Comment #17
jeffschulerRe-rolled again for current HEAD, and resolved an issue that testbot was choking on.
@catch, I'm not very familiar... If you could please give me more info on use "for the forum select to exclude containers", I'll see what I can do.
Comment #19
jeffschulerMissed the change from
$formto$elementin_form_builder_handle_input_element().Comment #21
catchWould be good if this could be re-rolled with -up to get function names for context.
Also we should apply this to forum containers to remove some horribly validation code.
Comment #22
cburschkaHere's the -up.
Comment #23
tomsherlock commentedForgive my ignorance; i'm not familiar with the proper way to construct patches.
Is this a typo:
@@ -2290,6 +2295,11 @@ function form_process_checkboxes($elemen
Should that be element?
Comment #24
effulgentsia commentedSeems same / very similar to #284917: Allow FAPI select, radios, and checkboxes to specify some options as disabled, so let's join forces over there. Marking this one as the duplicate rather than that one, because I prefer the use of a new property #disabled_options as is done on that issue rather than overloading #disabled.
Comment #25
jonhattan