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

TapocoL’s picture

If I remember right, it should be something like this.

$form['cbs'] = array(
  '#type' => 'checkboxes',
  '#options' => array(1 => "One", 2 => "Two", 3 => "Three"),
  '#default_value' => array(1, 3),
);

This will leave the options with keys 1 & 3 selected.

-Craig Jackson
-Web Developer

anschinsan’s picture

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.

geshan’s picture

does it work for AHAH generated checkboxes?? I'm having problem with that.

caspercash’s picture

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:

$form['choices'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Choose Choice'),
    '#options' => function_that_returns_an_array_with_IDs_as_keys(),
    '#default_value' => array_keys(function_that_returns_an_array_with_IDs_as_keys()),
  );
anschinsan’s picture

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

mark_schaal’s picture

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.

function form_ux_form_alter(&$form, &$form_state, $form_id){
	switch($form_id){
		case "user_register_form":
			//Set Default Roles
				$form['account']['roles']['#default_value']=array(""=>5);
		break;
        }
}
blazetech’s picture

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,
);

divadu’s picture

In my case without the checked attribute does not work

$form['cbs'] = array(
  '#type' => 'checkboxes',
  '#options' => array(1 => "One", 2 => "Two", 3 => "Three"),
  '#default_value' => array(1, 3),
  '#attributes' => array('checked' => 'checked') // <-- Important for checkboxes
);
Dmitry Kolyshev’s picture

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