I have several entities that inherit from a base Widget
.
In a form I have the field widgets
which is a collection. This form is based on an entity, which has a 1:n
relationship to Widget
. What I want to do is (based on the sent data in widgets
) to create a specific subclass to validate and create the actual object.
In the form I defined the field like this:
$builder->add('widgets', 'collection', array(
'type' => new WidgetType(),
'allow_add' => true
));
So for example I have two subsclasses, ImageWidget
and MapWidget
. I created FormTypes
for these two entities and now the client is sending the following data (I am using FOSRestBundle):
...
"widgets": [
{
"widget_type": "map",
"stationId": 543647
},
{
"widget_type": "image",
"image": "data:image/png;data,...."
}
]
And based on the property widget_type
(which is the property used by Doctrine to differentiate between the widget types in the database), I want the form to create a MapWidgetType
for the first entry and a ImageWidgetType
for the second one.
Is this possible to to?
I am using Symfony 2.5
This bundle should solve your problem: https://github.com/infinite-networks/InfiniteFormBundle#polycollection