嵌入式表单集合:嵌入表单字段不会呈现

时间:2016-04-28 11:16:16

标签: forms symfony twig

我是这个主题的新手,我正在尝试在任务表单中嵌入一个Tag表单集合。我想复制页面http://symfony.com/doc/2.8/cookbook/form/form_collections.html上给出的简单示例。即使我复制给定的代码,它只显示任务表单的描述字段而不是标签表单的名称字段。我尝试过一些东西,但一直无法解决。 这是渲染页面的外观: Here is the screenshot of the browser output page

以下是树枝文件:

{% 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 %}

以下是控制器代码:

<?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(),
        ));
    }
}

以下是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',
        ));
    }
}

下面是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',
        ));
    }
}

以下是任务类

<?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;
    }
}

下面是Tag Class

<?php

namespace AppBundle\Entity;

class Tag
{
    public $name;
}

1 个答案:

答案 0 :(得分:2)

您无法两次呈现表单行。 在树枝模板中删除此行:{{ 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 %}