Build a form having a checkbox for each entity in a doctrine collection
Asked Answered
S

3

14

I'm displaying an html table for a filtered collection of entities and I want to display a checkbox in each row as part of a form which will add the selected entities to a session var.

I'm thinking that each checkbox should have the entity id as its value and I'll get an array of ids from the form field data (ok, so the value ought to be an indirect ref to the entity, but for the sake of simplicity).

I've tried creating a form Type with a single entity type field, mapped to the id property of the entity and embedded into another form Type which has a collection type field.

class FooEntitySelectByIdentityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('foo_id', 'entity', array(
            'required'  => false,
            'class'    => 'MeMyBundle:FooEntity',
            'property' => 'id',
            'multiple' => true,
            'expanded' => true
        ));
    }

# ...

and

class FooEntitySelectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('identity', 'collection', array(
            'type'   => new FooEntitySelectByIdentityType,
            'options'  => array(
                'required' => false,
                'multiple' => true,
                'expanded' => true,
                'attr'     => array('class' => 'foo')
            ),
        ));
    }

# ...

and in a controller the form is created with a collection of entities as the initial data

$form = $this
    ->createForm(
        new \Me\MyBundle\Form\Type\FooEntitySelectionType,
        $collection_of_foo
    )
    ->createView()
;

When the form is rendered there is a single label for the identity field, but no widgets.

Is it even possible to use entity and collection type fields in this particular way? If so, what might I be doing wrong?

Shellfish answered 1/2, 2013 at 2:31 Comment(0)
S
9

I think this will answer your question.

Forget the FooEntitySelectionType. Add a property_path field option to FooEntitySelectByIdentityType and set the data_class option to null:

class FooEntitySelectByIdentityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('foo_id', 'entity', array(
            'required'      => false,
            'class'         => 'MeMyBundle:FooEntity',
            'property'      => 'id',
            'property_path' => '[id]', # in square brackets!
            'multiple'      => true,
            'expanded'      => true
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'      => null,
            'csrf_protection' => false
        ));
    }

# ...

and in your controller, build the FooEntitySelectByIdentityType:

$form = $this
    ->createForm(
        new \Me\MyBundle\Form\Type\FooEntitySelectByIdentityType,
        $collection_of_foo
    )
    ->createView()
;

and then in the controller action which receives the POSTed data:

$form = $this
    ->createForm(new \Me\MyBundle\Form\Type\FooEntitySelectByIdentityType)
;
$form->bind($request);
if ($form->isValid()) {
    $data = $form->getData();
    $ids  = array();
    foreach ($data['foo_id'] as $entity) {
        $ids[] = $entity->getId();
    }
    $request->getSession()->set('admin/foo_list/batch', $ids);
}

and finally, in your twig template:

{# ... #}
{% for entity in foo_entity_collection %}
    {# ... #}

    {{ form_widget(form.foo_id[entity.id]) }}

    {# ... #}
Shellfish answered 1/2, 2013 at 19:11 Comment(4)
that works! the downside to this whole method is that $form->getData() returns an array containing a collection of entities rather than plain integer ids, but I suppose the upside is that there can be no invalid ids saved to the session. thanks @jah!Shellfish
Be aware that this approach doesn't work, if you try to use strings as identifiers. The EntityChoiceList will then create integer based indices for the children and the form_widget call will throw an exception. :(Innominate
I'm running into this trouble too. Is there a more simple way to solve this simple problem?Scapolite
this solution is very slow with many entities, see my other question #42470402Picturesque
H
3

If someone is looking for solution for Symfony >=2.3

You have to change this:

    $data = $form->getData();
    $ids  = array();
    foreach ($data['foo_id'] as $entity) {
        $ids[] = $entity->getId();
    }

to this:

    $data = $form['foo_id']->getData();
    $ids = array();
    foreach ($data as $entity) {
        $ids[] = $entity->getId();
    }
Homunculus answered 12/4, 2014 at 9:23 Comment(0)
N
2

I create a symfony bundle for render a collection of entities as a checkbox table in a configurable way. Although it has been a while since this question was asked, I hope this can help others with this same kind of problem.

Njord answered 18/4, 2016 at 14:0 Comment(2)
I'm using this Bundle and I have to say it's pretty cool! Thanks!Lumumba
how is the performance of your bundle with trying to display up to 1000 records at once? I am planning on using datatables server side processing to limit how many records are shown on each page load, however it seems as though when I use the above other answers I need to create my entity type with all possible options it could expect, not just initially limited to the first page of records. I am having performance issues related to this.Picturesque

© 2022 - 2024 — McMap. All rights reserved.