3

I am able to create a new node with body field, but when I try to set the value for my entity reference field, I get

{"form_errors":{"field_drawing][und":"An illegal choice has been detected. Please contact the site administrator."}}

I am sending a structure similar to the working body field, but with 'target_id' instead of 'value':

'field_drawing' = {
    'und' = [
        0 = {
            'target_id' = 123
        }
    ]
}

123 is the nid of an existing node. What would cause an 'illegal choice' response?

3 Answers 3

4

I figured out the solution from comments on this issue.

To set the field via services, you can set the field's widget type to "autocomplete" and instead of just sending the nid, send a string with the title of the referenced node, a space, and the nid inside parenthesis.

'field_drawing' = {
    'und' = [
        0 = {
            'target_id' = 'Title of Node (123)'
        }
    ]
}
2
  • 2
    the title of Node can actually be any text. Only the nid matters when storing the field. This can save you from extra work trying to retrieve the node's title before calling the service.
    – LKallipo
    Oct 29, 2014 at 10:50
  • @LKallipo is correct, you can pass the value of '... (123)' as a string for thetarget_id property instead: Jun 22, 2016 at 6:37
2

In my case, an external script had to create nodes using json messages so I wanted to only pass the ids of the referenced nodes. to do this you have to switch the widget type to 'select list' and send the following:

'field_drawing':{
    'und':[123,124]            
}
0

Just for reference.

I got problem with such data structure in my project, using entityreference field in Entityform entity.

This json fragment:

..."field_f_s_culture":{"und":[{"target_id":"... (336)"}]},...

triggers next error:

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '... (336)' for column 'field_f_s_culture_target_id' at row 1)

I was have to revert entityreference field structure to:

..."field_f_s_culture":{"und":[{"target_id":"336"}]},...

to get it work.

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.