0

I am new to symfony. Trying to create an add and edit form with it. When I tried to add a select box in which the choice data is populated from database, but the select box shows only id as the value and name of option list

 $form = $this->createFormBuilder($defaultData)
            ->add('category_id', ChoiceType::class, array(
                    'label' => 'Category',
                    'choices'  => array(
                    $category))) 

This is my controller portion

   <label for="exampleInputEmail1"> Category</label>
                    {{ form_widget(form.category_id, {'attr' : { 'class':'form-control'}}) }}
              </div>

my output is

   <option value="id">id</option>

Please help me to display name too

 <option value="id">Name</option>
Ajzz
  • 340
  • 1
  • 7
  • 22

1 Answers1

0

Just because data is populated from db. Then instead of ChoiceType::class change to EntityType::class

 $form = $this->createFormBuilder($defaultData)
        ->add('category_id', EntityType::class, array(
                'label' => 'Category',
                'choices'  => array(
                $category))) 

check official doc EntityType Field

habibun
  • 1,552
  • 2
  • 14
  • 29