P
P
Prudkovski2014-11-10 17:11:04
symfony
Prudkovski, 2014-11-10 17:11:04

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);

ProfilePhonesFormType is like this:
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('telephones', 'collection', array(
            'type'         => new TelephoneFormType(),
            'allow_add'    => true,
            'allow_delete' => true,
            'by_reference' => false,
        ));

in the template I display the records of the collection like this:
<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>

In this case, all records of the collection are in the form (plus delete links, add from the manual from the cookbook). And it is necessary to render in the form only a certain record. I pass its id to the controller, but I don’t know what to do with it next. Or in the template you need to somehow hide all records with a different id?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Benkovsky, 2014-11-10
@prudkovski

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);

and work as you please. But it's better, as I wrote above, throw out the collection altogether

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question