Programmatically set value of Boolean radio button field in form I am using a custom field in the profile2 module for when a user registers. The field is a Boolean field using the radio button widget.

I want to programmatically set the value for the field using hook_form_alter().

I have managed to successfully set the #default_value using: 

function mymodule_form_user_register_form_alter(&amp;$form, &amp;$form_state) {    
  $form['profile_company']['field_employees']['und'][0]['value']['#default_value'] = 0;
}


But this code does not actually check the No button, so it looks like nothing has been selected and when the form is submitted it throws an error saying that that field is required and nothing has been selected. 

How can I get the code to actually check one of the buttons as well as setting the default value, based on values that I want to pre-fill the form with?

The code above would work fine with a text field, It just doesn't work with a Boolean field using radio buttons.

I have spent a while on this and can't seem to find an answer. I thought maybe I need to use the field #after_build property or possibly JavaScript?

I'm attaching a screen grab of my field settings.
First, check you are setting the #default_value property at a correct level in the $form array. It seems quite weird to get multiple values in $form['profile_company']['field_employees']['und'], so I'm not sure about assigning defaults nested under [0]['value'] (but I could be wrong). 

A good rule is to ensure you set #default_value at the same level as the #options array. For example, if you would have these options under the lang code like :

print_r($form['profile_company']['field_employees']['und']);

# Output :
Array
(
    ['#options'] =&gt; Array (
        # ... =&gt; ...
    ),
    # ... =&gt; ...
);


... you would set the #default_value at the same level : 

$form['profile_company']['field_employees']['und']['#default_value'] = $default_value;




Secondly, ensure the #default_value is mapped to an existing key in the form #options array : 

# Let's say your options are defined as : 
$form['profile_company']['field_employees']['und']['#options'] = array(
  0 =&gt; t('N/A'),
  1 =&gt; t('No'), 
  2 =&gt; t('Yes')
);

# This would set 'N/A' as default :  
$form['profile_company']['field_employees']['und']['#default_value'] = 0;