-->

using sonata_type_collection against a custom form

2019-04-02 05:24发布

问题:

is it possible to use sonata_type_collection against a custom form type instead of a property/relationship with another entity?

Something like this:

$formMapper->add('foo', 'sonata_type_collection',
    array('type' => new \myVendor\myBundleBundle\Form\Type\BlockType() 
));

Which throws me the following error

The current field `contingut` is not linked to an admin. Please create one for the target entity : ``

EDIT:

Something like this did the trick:

$formMapper->add('contingut', 'collection', array(                                 
    'type' => new \myVendor\myBundleBundle\Form\Type\Block1Type(),
    'allow_add' => true,
    'allow_delete' => true,
    'by_reference' => false 
));

回答1:

Instead of a custom type in your example, you can also use a native entity type.

$formMapper->add('cars',
                            'collection',
                            array(
                                'type' => 'entity',
                                'allow_add' => true,
                                'allow_delete' => true,
                                'by_reference' => false,
                                'label' => false,
                                'options' => array(
                                    'class' => 'YourBundle:Car',
                                    'property' => 'name',
                                    'label' => false
                                )
                            )
                        )