Answer the question
In order to leave comments, you need to log in
Symfone 2 Form: How to edit a specific collection entry?
Good afternoon.
I can't figure out how to approach the problem.
The point is. Made according to the manual symfony.com/doc/current/cookbook/form/form_collect... a collection editing form, adds and removes collection entries ok. (There are 2 entities: User and Telephone, OneToMany relationship). Now the task is to make a form in which you can edit (and delete) a specific entry from the collection. It turns out to either edit the entire collection in the form or edit a specific record (if rendered without a collection), but then it is impossible to delete, because validation does not pass.
So I can't figure out where to start. Tell me where to dig.
PS. Perhaps I will write in more detail how I do it and what does not work.
Now in the controller I render the form
$formPhone = $this->container->get('form.factory')->create(new ProfilePhonesFormType(), $user);
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('telephones', 'collection', array(
'type' => new TelephoneFormType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
));
<ul class="telephones" data-prototype="{{ form_widget(formPhones.telephones.vars.prototype)|e }}">
{% for phone in formPhones.telephones %}
<li class="added-telephone">{{ form_row(phone.phone_number) }}</li>
{% endfor %}
</ul>
Answer the question
In order to leave comments, you need to log in
Apparently, you used form_collection in vain. If you need to edit 1 record, then you are welcome to the usual CRUD. That is, a sign, add an entity (phone), delete, view, edit. form_collection is made that way on purpose.
But, as always, there are options. Since you have a phone id, please
$telephone = $repository->findById($telephoneId);
$telephoneForm = $this->container->get('form.factory')->create(new TelephoneFormType(), $telephone);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question