I would like to display in my form a list of elements from the entity. Near each element, it should be a delete button which will delete the specific element from the entty.
Do you know how I could do this?
Example: 3 entities: Person, Affiliation and PersonAffiliations. A person has 0 or many affiliations. Lets say I created 10 affiliations for the person and now I would like to delete one of them. How to create the form in order to display a list of all the affiliations for the specific person and near each affiliation display a small delete icon. When click on this icon, the affiliation should be deleted for this person.
You have to embed a collection of forms and use a lot of Javascript. It is in the Symfony2 Cookbook!
Follow the example with the tags provided in the link above! Its easy, you have to put one anchor with the "remove" or "x" string per each sub-form to be removed and handle the deletion with Javascript.
Form insertion is done by using the Prototype of the subform that you need to add, but seems you don't need insertion.
Notice that in your example you have to create an empty Person
object and to add to it the list of predefined Affiliation
object(s) such that the FormBuilder
will be capable to render the forms related to Affiliation
. Then you can delete the sub-forms with Javascript. Of course, it is in general better to define dedicated form types.
If you only need to deselect a list of entities that are in relations with the main entity, then you can use the entity field type and you can render is as a list of checkboxes with the expanded option. Your controller will bind the entity and the related relationships and so it Symfony will cancel the precedeing relationships.