title: How to Embed a Collection of Forms in Symfony?
body: <p>I am having some issues when trying to create a form that embeds a collection of another entity. </p>
  
  <p>I want to create a Recipe and for that purpose I want the user to be able to insert several Tags for that Recipe.</p>
  
  <p>I've followed the current <a href="http://symfony.com/doc/current/form/form_collections.html" rel="nofollow noreferrer">Symfony documentation</a> and I am actually able to insert both entities Recipe and Tag, but for Tag entity, only id is actually inserted.</p>
  
  <p>Here is what I've done so far:</p>
  
  <p><strong>RecipeType</strong> form: </p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('name')
          -&gt;add('recipe_tags', CollectionType::class, array(
              'entry_type' =&gt; TagsType::class,
              'allow_add' =&gt; true,
              'by_reference' =&gt; false,
          ));
  
  }
  </code></pre>
  
  <p>Which adds the collationType <strong>TagsType</strong> form that it's coded as follows:</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder-&gt;add('name');
  }
  </code></pre>
  
  <p>Then I create a tag object in the <strong>controller</strong> beforehand so the recipe entity has a default tag that will be updated after submit the form.</p>
  
  <pre><code>    $recipe = new Recipe();
      $tag = new Tags();
      $tag-&gt;setName('tag1');
      $recipe-&gt;addRecipeTag($tag);
      $form = $this-&gt;get('form.factory')-&gt;createNamed(null, RecipeType::class, 
      $recipe);
  </code></pre>
  
  <p>And finally, the <strong>Recipe Entity</strong> with the (I think so) proper information:</p>
  
  <pre><code>/**
   * @var \Doctrine\Common\Collections\Collection
   *
   * @ORM\ManyToMany(targetEntity="Tags", inversedBy="idRecipe", cascade=
   {"persist"})
   * @ORM\JoinTable(name="recipe_tags",
   *   joinColumns={
   *     @ORM\JoinColumn(name="id_recipe", referencedColumnName="id_recipe")
   *   },
   *   inverseJoinColumns={
   *     @ORM\JoinColumn(name="id_tag", referencedColumnName="id_tag")
   *   }
   * )
   * @Groups({"recipe_detail"})
   *
   */
  private $recipe_tags;
  </code></pre>
  
  <p>I added the cascade persist annotation following the documentation as well. Here is the method for adding tags.</p>
  
  <pre><code>/**
   * Add recipeTag
   *
   * @param \AppBundle\Entity\Tags $recipeTag
   *
   * @return Recipe
   */
  public function addRecipeTag(\AppBundle\Entity\Tags $recipeTag)
  {
      $this-&gt;recipe_tags[] = $recipeTag;
  
      return $this;
  }
  </code></pre>
  
  <p>And this is what I get inserted into my database (is the return after the POST request):</p>
  
  <pre><code> {
  "recipe": {
  "idRecipe": 3671,
  "name": "TagName#76232",
  "description": null,
  "duration": null,
  "updatedAt": null,
  "createdAt": null,
  "active": null,
  "image": null,
  "portions": null,
  "idCategory": null,
  "createdBy": null,
  "recipeTags": [
    {
      "idTag": 143,
      "name": null
    }
  ],
  "recipeSteps": [],
  "recipeIngredients": null
  </code></pre>
  
  <p>}
  }</p>
  
  <p>As you can see, the recipe is corretly added, but no name is added for the tag I just created. Plus,the pivot table <strong>recipe_tags</strong> does not add data at all.</p>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I think the problem was that I was using the same form name field for both entities. Now I've changed the field and a new error appears:</p>
  
  <pre><code>{
    "code": 400,
    "message": "Validation Failed",
    "errors": {
    "errors": [
      "This value is not valid."
     ],
    "children": {
      "name": [],
      "recipe_tags": {
        "children": [
          {
            "children": {
              "tag": []
          }
        }
      ]
    }
  }
  </code></pre>
  
  <p>}
  }</p>
  
  <p>And this is the parameters I am passing with the POST request:</p>
  
  <pre><code>{"name":"Recipe#2342"}
  {"recipe_tags":"TagName#76232"}
  </code></pre>
  
  <p>Any ideas? Thank you.</p>