Create a custom field type having CollectionType as parent

I have an SmsType, EmailType and VoiceCallType all containing the following collection:

            ->add('answers', CollectionType::class, [
                'label'         => 'form.communication.fields.answers',
                'entry_type'    => AnswerType::class,
                'entry_options' => [
                    'label' => false,
                ],
                'allow_add'     => true,
                'allow_delete'  => true,
                'delete_empty'  => true,
                'prototype'     => true,
                'required'      => false,
                'attr'          => [
                    'class' => 'collection',
                ],
            ])

In order to simplify, I created an AnswersType having the CollectionType as parent:

<?php

namespace AppFormType;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
use SymfonyComponentOptionsResolverOptionsResolver;

class AnswersType extends AbstractType
{
    public function setDefaultOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'label'         => 'form.communication.fields.answers',
            'entry_type'    => AnswerType::class,
            'entry_options' => [
                'label' => false,
            ],
            'allow_add'     => true,
            'allow_delete'  => true,
            'delete_empty'  => true,
            'prototype'     => true,
            'required'      => false,
            'attr'          => [
                'class' => 'collection',
            ],
        ]);
    }

    public function getParent()
    {
        return CollectionType::class;
    }
}

And use ->add('answers', AnswersType::class) on every form types instead of the big block.


For some reason, the resulting markup is not the same:

Without the AnswersType, form_row() generates the following markup:

<fieldset class="form-group"><div id="campaign_trigger_answers" class="collection" data-prototype=" &lt;div class=&quot;row answer-row&quot; style=&quot;margin-bottom: 5px;&quot; id=&quot;answer-nb-__name__&quot;&gt; &lt;div class=&quot;col-10&quot;&gt; &lt;input type=&quot;text&quot; id=&quot;campaign_trigger_answers___name__&quot; name=&quot;campaign[trigger][answers][__name__]&quot; placeholder=&quot;Saisir une réponse&quot; class=&quot;answer-input form-control&quot; /&gt; &lt;/div&gt; &lt;div class=&quot;col-2 text-right&quot;&gt; &lt;a href=&quot;#&quot; data-index=&quot;__name__&quot; class=&quot;remove-answer btn btn-danger&quot;&gt;X&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; "></div></fieldset>

With the AnswerType, form_row() generates the following markup:

<fieldset class="form-group"><div id="campaign_trigger_answers"></div></fieldset>

As you can see, there is no prototype attribute generated anymore with the AnswersType, even though I’ve set the option to true. Where is my mistake?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *