簡體   English   中英

Symfony 4 Form Collection與原型

[英]Symfony 4 Form Collection with prototypes

我是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
        ]);
    }
}

我對這段代碼感到$contact->getTags()->add($tag3); => $contact->getTags()->add($tag3); 看起來Tags是一個實體並Contact另一個實體,因此您的Contact實體應該有adders / removers adders和/或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)
    {
        // ...
    }
}

實現案例的一個很好的例子: 如何嵌入表單集合

然后我不知道你的TagType表格是什么樣的,但即使它很好地開發你的樹枝也不行。

第一個form_widget(form)呈現整個表單

來自Symfony Doc

呈現給定字段的HTML小部件。 如果將此應用於整個表單或字段集合,則將呈現每個基礎表單行。

因此重新渲染集合將不起作用。 即使你的枝條代碼不是很好的渲染集合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM