Reputation: 9269
I'm using Symfony 3.3 and I have a form with a CollectionType like :
$builder->add('links', CollectionType::class, array(
'label' => false,
'entry_type' => LinkType::class,
'entry_options' => ['data_class' => CompanyLink::class],
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
));
I followed the documentation : https://symfony.com/doc/current/reference/forms/types/collection.html
This is what I have in my view :
So, the form displays well data already in my database (one line each time) but it always adds an empty field in addition.
How can I remove this empty field? Because I want to have this line only I click on the button "Ajouter".
Thanks!
Upvotes: 1
Views: 2247
Reputation: 51
I think in your function of rendering form you have setData(),
to render the form without data, you just need to do this sample:
//Your main entity
$mainEntity = new Entity();
//adding the other entity into collection variable of other entity of main entity
$mainEntity->addOtherEntity(new otherEntity());
$form = $this->createForm(
MainEntityForm::class,
$mainEntity
);
hope it helps.
Upvotes: 1
Reputation: 37
This post is a bit old ihope it's not to late. I found a solution, even if you travel the collectionType field with a twig for. To avoid having this empty field after your for loop, add
{% do form.your_field.setRendered %}
It worked for me, hope it will do the same for you :)
Upvotes: 0