Search code examples
phpsymfonysymfony4symfony-forms

Symfony 4 : choiceType doesn't show choice_value correctly


I'm new with Symfony and I want to build a select option using choiceType.

So I add this code to buildForm method :

$builder->add('applications', ChoiceType::class, [
  'choices' => [
    '-- Choisir --' => NULL,
    'Admin' => '2',
    'Super' => '1',
    'visiteur' => '10',
    'admin local' => '98',
    'partenaire gestionnaire' => '16',
    'admin national' => '15',
    'soutien' => '11',
    'exploitant' => '12',
    'moe' => '99',
  ]
]);

But when I inspect HTML code I don't have the correct numbers :

<select id="user_applications" name="user[applications]" class="form-control">
  <option value="0" selected="selected">-- Choisir --</option>
  <option value="1">Admin</option>
  <option value="2">Super</option>
  <option value="3">visiteur</option>
  <option value="4">admin local</option>
  <option value="5">partenaire gestionnaire</option>
  <option value="6">admin national</option>
  <option value="7">soutien</option>
  <option value="8">exploitant</option>
  <option value="9">moe</option>
</select>

Solution

  • To add some context to Le Professionnel's answer: If you take a look at the ChoiceType documentation, you can see that the type allows you to define a custom placeholder. Try changing your code to this:

    $builder->add('applications', ChoiceType::class, [
        'placeholder' => '-- Choisir --',
        'choices' => [
            'Admin' => '2',
            'Super' => '1',
            'visiteur' => '10',
            'admin local' => '98',
            'partenaire gestionnaire' => '16',
            'admin national' => '15',
            'soutien' => '11',
            'exploitant' => '12',
            'moe' => '99',
        ]
    ])