You'd think this would be easy. I'm calling drupal_form_submit('user_register_form', $form_state) to automatically register users after they have completed filling out a Webform.

But I have some Entity Reference fields in my user profile and form_validate is not happy with what I am submitting for them. All of the ER fields are references to taxonomy terms. Some of them are single-value fields, some of them are unlimited value fields. They have various widgets attached such as Select List, Autocomplete, Checkboxes and Radio Buttons. Some of them are Required fields, others are not.

I've tried setting all of the following in $form_state['values'] before submitting the form:

$form_state['values']['field_foo']['und'] = $tid;
$form_state['values']['field_foo']['und'][0] = $tid;
$form_state['values']['field_foo']['und'][0]['target_id'] = $tid;

But Drupal still throws out 'An illegal choice has been detected' when it comes to validate the field, or sometimes a 'Field is required' error.

What am I doing wrong and how do I go about figuring out the right way to specify each of the fields?

1 Answer 1


I think form might be expecting in proper format that is "[entity-title] [entity_id]", so field array should look like.

'field_foo' = {
    'und' = [
        0 = {
            'target_id' = 'Title of entity ($tid)'

Just to make sure you can form alter user form, add a custom validate and do a dsm($form_state);

  • This looks the same as $form_state['values']['field_foo']['und'][0]['target_id'] = $tid; which I have tried, but unfortunately had no success with. Feb 18, 2017 at 11:38
  • make sure you are not just assigning tid, it should be title with tid with space in between.
    – arpitr
    Feb 18, 2017 at 11:43

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.