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(&$form, &$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.

enter image description here

  • Wouldn’t the default value for the disabled state be “No” according to your settings? What’s the value of the input in the rendered html? That’ll tell you for sure
    – Clive
    Commented Nov 17, 2018 at 12:43
  • Thanks for your comment Clive. Please see my comment below the answer. I had been making a silly mistake and been targeting the wrong field in my code!
    – Ben
    Commented Nov 17, 2018 at 14:06

1 Answer 1


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 :


# Output :
    ['#options'] => Array (
        # ... => ...
    # ... => ...

... 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 => t('N/A'),
  1 => t('No'), 
  2 => t('Yes')

# This would set 'N/A' as default :  
$form['profile_company']['field_employees']['und']['#default_value'] = 0;
  • Thank you so much for taking the time to look at my question. I was making a silly mistake. I had two employee fields and I had targeted the wrong one and thought I was targeting the other one. Everything works now. Your answer helped me to see where I had gone wrong. I changed my line of code to: $form['profile_company']['field_fewer_250_employees']['und']['#default_value'] = 1; and it all works fine now. Thanks again.
    – Ben
    Commented Nov 17, 2018 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.