user10328808
user10328808

Reputation: 3

Set Value to Placeholder option of EntityType in symfony

I am a beginner in symfony. Is there any way to set a value to my placeholder in EntityType of formtype My current formtype is

$builder
    ->add('scheme', EntityType::class, array(
        'class' => 'AppBundle:Lottery\Masters\MasterScheme',
        'placeholder' => '-- Select Scheme --',
        'choice_label' => 'schemeName',
        'attr' => array(
            'class' => 'select'
        )
    )
);

And when I inspect my dropdown there is no value for the placeholder ---Select Scheme-- .Is there is any way to give value to my placeholder Hope i Get a solution

Upvotes: 0

Views: 2854

Answers (1)

Anjana Silva
Anjana Silva

Reputation: 9201

Are you looking for an empty value? So, it will be popluated as the first thing in the drop down. Then you can like this,

$builder->add('name', null, array(
    'required'   => false,
    'empty_data' => 'John Doe',
));

https://symfony.com/doc/current/reference/forms/types/entity.html#empty-data

If you meant the Label (a text next to the drop down), use this

$builder->add('users', EntityType::class, array(
    'class' => User::class,
    'query_builder' => function (EntityRepository $er) {
        return $er->createQueryBuilder('u')
            ->orderBy('u.username', 'ASC');
    },
    'choice_label' => 'username',
));

https://symfony.com/doc/current/reference/forms/types/entity.html#using-a-custom-query-for-the-entities

Upvotes: 2

Related Questions