Litsher - Nathan
Litsher - Nathan

Reputation: 275

Symfony 4, Add Type as form collection type

I am trying to add a form type as the entry type for my collectionType. This type and the related entity does have several fields.

I have tried to build it with several different configurations and this is my most recent attempt.

            ->add(
            'article_prices',
            CollectionType::class,
            [
                'entry_type'    => ArticlePriceType::class,
                'allow_add'     => true,
                'allow_delete'  => true,
                'by_reference'  => false,
                'prototype'     => true,
                'entry_options' => [
                    'required' => true,
                ],
            ]
        );

I was hoping that I would get a builder for the fields in my type (ArticlePriceType) but I am just getting the label when adding this to my form :

{{ form_row(form.article_prices) }}

Is it possible to get it automaticly build the fields in the type or do I have to build in myself in TWIG?

Upvotes: 0

Views: 525

Answers (1)

balzacLeGeek
balzacLeGeek

Reputation: 815

Try this:

->add('article_prices', CollectionType::class, [
    'entry_type'   => ChoiceType::class,
    'entry_options'  => [
        'label' => false,
        'choices' => [
            'MyField_1' => ArticlePriceType::ARTICLE_TYPE_1,
            'MyField_x' => ArticlePriceType::ARTICLE_TYPE_x,
            'MyField_n' => ArticlePriceType::ARTICLE_TYPE_n,
        ],
    ],
])

And inside your ArticlePriceType Entity you must have the ARTICLE_TYPE_1, ARTICLE_TYPE_x and ARTICLE_TYPE_n constant variables

Upvotes: 1

Related Questions