Search code examples
jsonsymfonymany-to-manysymfony-formsfosrestbundle

REST API: how to post a manytomany relationship with fosrestbundle


In my Symfony 4 project, I have 2 entities: Question and PossibleAnswer.
A question can have many possible answers and an answer can be used in many different questions.

I want to POST a question, I use FOSRestBundle.
This is how I would format the body of my request:

{
    "title": "my question",
    "possibleAnswers": [
        {
            "id": 1,
            "title": "my first answer"
        }
    ]
}

At least this is how FOSRestBundle formats the answer when I create the data directly in the database and GET the question.

But I keep getting the same error:

Validation Failed, possibleAnswers This value is not valid

I use Symfony forms to validate my POST and the fieldType I use for the possibleAnswers field of Question is an EntityType with the option multiple set to true. Maybe that's where the error comes from.

So my question is:
How do I format the body of my request to add a valid possibleAnswers field ? and if I am doing it right then, which form type should I use for it to work ?

Sorry for not adding any code, the context is actually quite more complex but I'm pretty sure either the JSON Format of my field or the form type is wrong.

[edit]

here are all the form types I tried for the possibleanswers field

//->add('possibleAnswers', EntityType::class, array('data' => [], 'required' => false, 'multiple' => true, 'class' => PossibleAnswer::class))
  ->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => EntityType::class, 'entry_options' => array('class' => PossibleAnswer::class)))
//->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => PossibleAnswerType::class))
//->add('possibleAnswers', CollectionType::class, array('required' => false, 'entry_type' => PossibleAnswerType::class, 'entry_options' => array('class' => PossibleAnswer::class)))

here are all the JSON formats I tried to select the possibleanswers:

{
    "title": "a postman created question",
    "answerConditions": [
        {
            "id": 1,
            "title": "already existing question",
        }
    ]
}

{
    "title": "a postman created question",
    "answerConditions": [
        {
            "possibleanswer_id": 1
        }
    ]
}

{
    "title": "a postman created question",
    "answerConditions": [
        {
            "id": 1
        }
    ]
}

{
    "title": "a postman created question",
    "answerConditions": [
        1
    ]
}

Solution

  • If you want to create the PossibleAnswer when posting the Question, you need to replace in your form the EntityType by a CollectionType.

    I often use it in my POST request when I want to post the sub-object (here the PossibleAnswer) when I post the parent (Question) :

    Question form

    $builder
        ->add('possibleAnswers', CollectionType::class, array(
            'entry_type'  => PossibleAnswerType::class,
            'allow_add' => true,
        ))
    ;
    

    PossibleAnswer form :

    $builder
        ->add('title')
    ;
    

    And the json looks like :

    {
        "title": "my question",
        "possibleAnswers": [
            {
                "title": "my first answer"
            }
        ]
    }
    

    Hope it can help.

    Edit : If you want to link a PossibleAnswer to a Question while creating you Question you can use this :

    Question form :

    $builder
        ->add('possibleAnswer', EntityType::class, array(
            'class' => YOUR_ENTITY::class,
            'multiple' => true
        ));
    

    And you json should only contains the id of the entity you want to link to. It should be like this :

    {
        "title": "a postman created question",
        "answerConditions": [
            1
        ]
    }