Hi Experts,
I've a problem with the forms API and the form element 'checkboxes' (multiple ones).
How do I get a single checkbox checked by default?
i.e. I have an array $options where the keys are some node id's, the values are node titles. I'll render the checkboxes with the keys/numbers as html input value. To do this, I put $form['#options'] = $options, $form['#type'] = 'checkboxes'.
Additional I have a variable $default_value - which is one node id/key/number of my $options array. I've tried to put it as $form['#default_value'] and/or $form['#value']. But I don't get the expected result. What am I doing wrong?
Additional information:
The form element I want to render is part of a webform (module: webform);
I try to adapt the code of irolo at http://www.irolo.net/drupal_webform_and_dynamic_checkboxes, the array I described is part of the function '_webform_render_dynamicselect' and ist named $form_item there. He had also put an example online at http://immaculata.bydesign-area51.com/registration.
It would be very nice, if somebody can help me!
Good night from Austria
anschinsan
Comments
If I remember right, it
If I remember right, it should be something like this.
This will leave the options with keys 1 & 3 selected.
-Craig Jackson
-Web Developer
I've tried this already
Thanks for your answer, but I've already tried to put $form[#default_value] - without getting the expected result.
I've tried as well setting the default value as an array. It didn't work.
does it work for AHAH
does it work for AHAH generated checkboxes?? I'm having problem with that.
How about if you don't know
How about if you don't know the start and end of the array keys? Like for example when the value of your #options is fetched from the database where the keys are the ID? How will you make all of the options selected?
UPDATE: hey! I've found my workaround i just put this in the default_value:
Worst debugger ever seen ...
Sorry for wasting your time! I was so desperate in debugging that I was running in the wrong direction -> changed the values in the wrong function.
Tapocol was right and I'm ashamed, sorry - but happy to see my checkboxes checked, finally :)
#default_value takes an associative array
Right now it looks as if you are passing a standard array. Try passing an associative array.
Below, I have demonstrated an example for adjusting the User Registration form, with the assumption there was a value for key "5" in the Roles options.
For checked all
Simple set attribute like this for checked all
$form['myfield'] = array(
'#type' => 'checkboxes',
'#title' => t('Name'),
'#default_value' => array(1),
'#attributes' => array('checked' => 'checked'),
'#options' => $options,
'#tree' => TRUE,
);
#attributes checked
In my case without the checked attribute does not work
A little correction
You need to use #default_value like this:
'#default_value' => array(1 => "One", 3 => "Three"),
This code
'#attributes' => array('checked' => 'checked')
set all checkboxes as checked