I am new to this topic and am trying to embedd a collection of Tag forms inside the Task Form. I am trying to duplicate the simple example given on the page http://symfony.com/doc/2.8/cookbook/form/form_collections.html . Even when I copy the given code, It just display the Task form's description field and not the Tag form's name field. I tried some things but haven't been able to figure it out.
This is how the rendered page looks:

Below is the twig file:
{% extends 'base.html.twig' %}
{% block body %}
<h3>Embedded Collection of Forms!</h3>
{{ form_start(form) }}
{{ form_row(form.description) }}
{{ form_row(form.tags) }}
<h3>Tags</h3>
<ul class="tags">
{% for tag in form.tags %}
{# {{ form_widget(tag) }} #}
<li>{{ form_row(tag.name) }}</li>
{% endfor %}
</ul>
{{ form_end(form) }}
{% endblock %}
Below is the controller code:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Task;
use AppBundle\Entity\Tag;
use AppBundle\Form\TaskType;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
$task = new Task();
// dummy code - this is here just so that the Task has some tags
// otherwise, this isn't an interesting example
$tag1 = new Tag();
$tag1->name = 'tag1';
$task->getTags()->add($tag1);
$tag2 = new Tag();
$tag2->name = 'tag2';
$task->getTags()->add($tag2);
// end dummy code
$form = $this->createForm(new TaskType($task));
$form->handleRequest($request);
return $this->render('default/index.html.twig', array(
'form' => $form->createView(),
));
}
}
Below is the TaskType.php
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('description');
$builder->add('tags', CollectionType::class, array(
'entry_type' => TagType::class
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Task',
));
}
}
Below is the TagType.php
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TagType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Tag',
));
}
}
Below is the Task Class
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
class Task
{
protected $description;
protected $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
public function getDescription()
{
return $this->description;
}
public function setDescription($description)
{
$this->description = $description;
return $this;
}
public function getTags()
{
return $this->tags;
}
}
Below is the Tag Class
<?php
namespace AppBundle\Entity;
class Tag
{
public $name;
}
$form = $this->createForm(new TaskType($task));change that to either$form = $this->createForm(new TaskType(), $task);or$form = $this->createForm(TaskType::class, $task);i will take a closer look later