Why are default values in a checkboxes $form not displayed? I'm having a drupal form (7.x) of type checkboxes.

I have an array of values for my checkboxes.

$options = array(
    'uid'         =&gt; 'UID',
    'name'        =&gt; 'Name',
    'mail'        =&gt; 'E-mail',
    'created'     =&gt; 'Created',
    'edit_node'   =&gt; 'Edit',
    'cancel_node' =&gt; 'Cancel',
);


I have an array of default selected values.

$defaults = array(
    'uid'         =&gt; 'UID',
    'name'        =&gt; 'Name',
);


And now I try to make the default values selected by default, like this:

$form['dynamic_user_fields'] = array(
    '#type' =&gt; 'checkboxes',
    '#title' =&gt; t("Dynamic User Fields"),
    '#options' =&gt; $options,
    '#default_value' =&gt; $defaults,
    '#description' =&gt; t("Select the fields to display in the view."),
);


But this is not working, all checkboxes are unchecked by default. Where is my mistake?



What is not working so far: I've seen this related question but the answers do not work.

What I've tried:

(1) using numeric keys does not work.

'#default_value' =&gt; array(1, 2),




(2) using array keys does not work.

'#default_value' =&gt; array('uid', 'name'),




(3) using array structure does not work.

'#default_value' =&gt; array('uid' =&gt; 'UID', 'name' =&gt; 'Name')




Any ideas?



References


This is the full code I'm Using: views_handler_filter_dynamic_fields.inc#l317
This is a demo site: drupal-7/dynamic-user-fields
Related issue: #1245838 Default fields should be checked initially


I stepped up to maintain this module but the code base is a migrated D6 module.
For form elements expecting multi-value defaults, Drupal expects an array where keys and values are equal to the keys in your options array. Something like the following should work:

$defaults = array(
    'uid' =&gt; 'uid',
    'name' =&gt; 'name',
);


You may also use $defaults = drupal_map_assoc(array('uid', 'name'));.