samedi 28 février 2015

Change symfony2 form field but keep relation

So I have a form that is based on an entity that contains a one to many relationship.


The problem is that this field is rendered as a select (or choice). I really don't want to load all the possible ids (there are many) but just want to load the one that is set in the entity (which is the id that appears selected in the select).


Is there any way of doing this and still keep the relationship? If I really have to change the field how can I access, in the form class, the selected entity given to the entity so that I can retrieve the id?


UPDATE


To make this a bit clearer here is my form code:



$this->createFormBuilder()
->add('items', 'collection', array(
'type' => new \MyBundle\Form\ItemsType(),
'allow_add' => true,
'data' => $itemsEntities
)
)
->add('submit', 'submit')


In the $itemsEntities I have 5 entities all of which generate the select with loads of ids. Hakins answer would work I think if this would be just one field but since there are many I don't really know how to handle this.


I have tried to put an eventListner on the \MyBundle\Form\ItemsType for but I can never access any data.


Aucun commentaire:

Enregistrer un commentaire