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 App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\OptionsResolver\OptionsResolver;
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=" <div class="row answer-row" style="margin-bottom: 5px;" id="answer-nb-__name__"> <div class="col-10"> <input type="text" id="campaign_trigger_answers___name__" name="campaign[trigger][answers][__name__]" placeholder="Saisir une réponse" class="answer-input form-control" /> </div> <div class="col-2 text-right"> <a href="#" data-index="__name__" class="remove-answer btn btn-danger">X</a> </div> </div> "></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?