I'm new with symfony and I want to build 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]" >
<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>
CodePudding user response:
You need to remove '-- Choisir --' => NULL, by another value, you can't use null value.
If a value is null or similar, symfony replaces all values by integer
The best practice is to use the placeholder attribute in your case
CodePudding user response:
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',
]
])