glochen G Uyirr
glochen G Uyirr

Reputation: 27

Empty option in a form query symfony 3.4

I must display an entity in a query_builder form

here is my code in the form type:

$builder->add('dispositif', EntityType::class, array(
        'class'     => 'LeaPrestaBundle:EgwDispositif',
        'label'     => 'nomDispositif',
        'required'  => true,
        'empty_data' => null,
        'query_builder' => function( $er)
        {
        return $er->createQueryBuilder('d')
        ->where('d.isActive = :isActive')
        ->setParameter('isActive', 1 )
        ->orderBy('d.nomDispositif','ASC');
        },
    ));

The problem is that the option is empty :

<select id="egw_presta_dispositif" name="egw_presta[dispositif]">
    <option value="52"></option>
    <option value="55"></option>
    <option value="62"></option>
    <option value="58"></option>
</select>

For example, aside the value 52, there should have a label for "dispositif" (name of dispositif).

Does it coming from the entity ?

Thanks in advance for your help!

Upvotes: 0

Views: 482

Answers (1)

Yoann MIR
Yoann MIR

Reputation: 821

Try adding a 'choice_label' property with the EgwDispositif field containing what you want to display:

$builder->add('dispositif', EntityType::class, array(
        'class'     => 'LeaPrestaBundle:EgwDispositif',
         /** label will be displayed next to your form field unless it's a translation key or else  **/
        'label'     => 'Nom du dispositif',
        'required'  => true,
        'empty_data' => null,
        /** any field containing what you want to display as option label  **/
        'choice_label' => 'nomDispositif',
        'query_builder' => function( $er)
        {
        return $er->createQueryBuilder('d')
        ->where('d.isActive = :isActive')
        ->setParameter('isActive', 1 )
        ->orderBy('d.nomDispositif','ASC');
        },
    ));

It seems like you were confusing the option 'label' of the form field property with the option 'choice_label' . You probably want your label to be something user will read (bu maybe not, it's up to you)

take a look here : https://symfony.com/doc/current/reference/forms/types/entity.html

Upvotes: 2

Related Questions