Angel
Angel

Reputation: 1980

Symfony 2.3 add form collections of entities

I'm triying to make a dynamic form adding collections inside an entity.

I have followed the code example in the Symfony's documentation, and it works, but what I want to do is add a new form (the form of the entity collections).

So, if I have an entity A that contains a collection of entities B, I want to add new entities B dynamically in the form, but I don't know how to do it.

The entity A form should be something like:

$builder->add('entityB', 'collection', array(
    'type'   => 'HOW TO PUT THE FORM OF THE ENTITY B???',
    'options'  => array(
        'required'  => false,
    ),
    'allow_add'=>true,
));

Upvotes: 0

Views: 9559

Answers (1)

Carrie Kendall
Carrie Kendall

Reputation: 11225

Taken from the Cookbook:

$builder->add('entityB', 'collection', array(
    'type' => new EntityBType(),
    'options'  => array(
        'required'  => false
    ),
    'allow_add' => true
));

This is assuming that you have created a Form Type Class for EntityB (not manually creating it when needed in your controller). The linked cookbook entry gives a lot of good examples based on per-case situations.

Upvotes: 3

Related Questions