2

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: Here is the screenshot of the browser output page

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;
}
2
  • 1
    inside your controller you have $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 Commented Apr 28, 2016 at 11:58
  • Thank you Dheeraj. This did fix the problem. :) Commented Apr 28, 2016 at 12:37

1 Answer 1

2

You can't render a form row twice. Remove this line in your twig template: {{ form_row(form.tags) }}

{% extends 'base.html.twig' %}

{% block body %}

    <h3>Embedded Collection of Forms!</h3>

    {{ form_start(form) }}
    {{ form_row(form.description) }}
    <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 %}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the info jahller
Hi @utkarsh2k2 if this has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.
Hey @Jahller, The info you provided was correct, however it did not solve the issue I was facing. As it turns out than Dheeraj's solution mentioned in the above comment did. But I did give your solution a +1. Thanks again :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.