Search code examples
formssymfonyentity

Symfony Forms - entity field with query_builder, no choices_as_values?


I am trying to achieve something very simple but not sure if this is supported, possible or unexpected behaviour. I have a Symfony entity field which loads some data based on the selection of another field. The data is loaded ok but I want the option name and value to be the same. At the moment, it is populating the names ok, but I want the values to be the same as the names (the choices_as_values option in a Symfony choice field). Is that possible in an entity field.

Here is an example code:

    $builder
        ->addEventListener(
            Form\FormEvents::POST_SET_DATA,
            function (Form\FormEvent $event) {
                $attributeData = $event->getData();

                $event->getForm()->add(
                    'group',
                    'entity',
                        array(
                        'class'         => 'AppBundle:CategoryAttributeData',
                        'placeholder'   => 'Select a data group',
                        'label'         => 'Attribute Group',
                        'choice_label'  => 'content',
                        'required'      => false,
                        'query_builder' => function (Repository\CategoryAttributeData $repository) use ($attributeData) {
                            $queryBuilder = $repository->createQueryBuilder('u')
                                ->select('u')
                                ->where('u.type = :type')
                                ->andWhere('u.group IS NULL')
                                ->setParameter('type', $attributeData->getType())
                            ;

                            return $queryBuilder;
                        }
                    )
                );
            }
        )
    ;

The output is:

<select id="attribute_data_group" name="attribute_data[group]" class="form-control">
<option value="">Select a data group</option>
<option value="1">Cars</option>
<option value="2">Electronics</option>
<option value="3">Furniture</option>
</select>

What I am trying to achieve is:

<select id="attribute_data_group" name="attribute_data[group]" class="form-control">
<option value="">Select a data group</option>
<option value="Cars">Cars</option>
<option value="Electronics">Electronics</option>
<option value="Furniture">Furniture</option>
</select>

Since this field is populated via an event listener (because it depends on another field value) I cannot add a view transformer in here.

Any suggestions?


Solution

  • This is actually more complicated than it would seem on the surface. The Entity form type assumes that the "values" of the choice selector are the unique identifier for an entity, this allows the form type (and its associated transformers) to find and transform the passed values from and to the relevant entities. So the first question is - can you uniquely identify your entity by the string "Cars", or "Electronics"? If you can't, then the next question is how were you planning on converting that string into the entity?

    If you can, then things are relatively easier. Effectively you need to provide a different ChoiceList implementation for the choice field type - this is effectively what the Entity type already does. I'm not familiar with the correct way to do this and I believe the method changed between Symfony 2.6 and 2.7, but I would look into classes like Symfony\Bridge\Doctrine\Form\Type\EntityType as well as the base Symfony\Component\Form\Extension\Core\Type\ChoiceType.

    Short version: it's not wholly straightforward but definitely possible.