【问题标题】:Symfony 4 Form Collection with prototypes带有原型的 Symfony 4 表单集合
【发布时间】:2018-11-21 17:09:18
【问题描述】:

我是 Symfony 4 的新手,我正在尝试使用带有数字选项的 ChoiceType 字段呈现表单,以便生成用户选择的确切标签数。

这是我的控制器:

class ContactController extends AbstractController
{
    /**
     * @Route("/matrix", name="matrix")
     */
    public function index(Request $request)
    {
        $contact = new Contact();
// i've already added some tags
        $tag3 = new Tag();
        $tag3->setName('tag3');
        $contact->getTags()->add($tag3);

        $tag4=new Tag();
        $tag4->setName('ciao');
        $contact->getTags()->add($tag4);

        $form = $this->createForm(ContactType::class, $contact);
        $form->handleRequest($request);


        if ($form->isSubmitted() && $form->isValid()) {

            $contactFormData = $form->getData();
            dump($contactFormData);
        }

        return $this->render('contact/index.html.twig', array(
            //'our_form' => $form,
        'form' => $form->createView(),
        ));
    }

在我的代码的这一点上,表格似乎已填满,我检查了一些转储。

这是我的树枝

{% block body %}
    <div>
        {{ form_start(form) }}
        {{ form_widget(form) }}
        <ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e('html_attr') }}">
            {% for tag in form.tags %}
                <li> {{ form_row(tag.name) }}
                </li>
            {% endfor %}
        </ul>
    <input type="submit" value="Send" class="btn btn-success" />
    {{ form_end(form) }}
</div>

{% endblock %}

这两个文件之间似乎没有可见性,实际上,他无法进入for循环。我已经倾倒了一些东西,我已经看到标签在这一点上没有孩子,但它应该。

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('motto')
            ->add('expectations', ChoiceType::class, array(
                'choices'  => array(
                    '1' => '1',
                    '2' => '2',
                    '3' => '3',
                    '4' => '4',
                    '5' => '5',


                ),
            ));

$builder->add('tags', CollectionType::class, array(
    'entry_type' => TagType::class,
    'entry_options' => array('label' => false),
    'allow_add' => true,
    'by_reference' => false,
    'mapped' => false,

));

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            // Configure your form options here
        ]);
    }
}

【问题讨论】:

标签: php html symfony twig symfony4


【解决方案1】:

我对这段代码感到困惑 => $contact-&gt;getTags()-&gt;add($tag3);。似乎Tags 是一个实体,而Contact 是另一个实体,因此您的Contact 实体应该具有adders/removers 和/或setters/removers(如果不需要同时累积两者的事件)。

所以你的实体应该喜欢:

class Contact
{
    // ...

    /** @var Collection */
    protected $tag;

    // ...

    public function __construct()
    {
        $this->tags = new ArrayCollection();
    }

    // ...

    public function addTag(Tag $tag)
    {
        $this->tags->add($tag);
    }

    public function removeTag(Tag $tag)
    {
        // ...
    }
}

一个很好的例子来实现你的案例:How to Embed a Collection of Forms

那我不知道你的 TagType 表单是什么样子的,但即使它发展得很好,你的树枝也不行。

首先form_widget(form)渲染整个表单

来自 Symfony 文档

呈现给定字段的 HTML 小部件。如果将此应用于整个表单或字段集合,则会呈现每个底层表单行。

所以重新渲染集合将没有效果。即使你的 twig 代码不是渲染集合的好代码。

【讨论】:

    猜你喜欢
    • 2019-05-23
    • 2021-10-08
    • 1970-01-01
    • 2020-07-18
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多