Introduction
For my personal project i use
- Symfony v4.2
- PHP 7.2.12
Problem
I can not figure out how to make ChoiceType element display IDs and EMAILs as HTML SELECT option values and indexes.
Example
The picture displays current state of html select with data (id and email) from database.
Code
Array of data from database
($this->userChoices)
array:4 [
0 => array:2 [
"id" => 7
"email" => "1st@example.com"
]
1 => array:2 [
"id" => 8
"email" => "2nd@example.com"
]
2 => array:2 [
"id" => 5
"email" => "3rd@example.com"
]
3 => array:2 [
"id" => 6
"email" => "4th@example.com"
]
]
What i am getting
<select id="my-select" name="my-select" class="form-control">
<optgroup label="0">
<option value="7">7</option>
<option value="1st@example.com">1st@example.com</option>
</optgroup>
<optgroup label="1">
<option value="8">8</option>
<option value="2nd@example.com">2nd@example.com</option>
</optgroup>
<optgroup label="2">
<option value="5">5</option>
<option value="3rd@example.com">3rd@example.com</option>
</optgroup>
<optgroup label="3">
<option value="6">6</option>
<option value="4th@example.comm">4th@example.com</option>
</optgroup>
</select>
What i want to get
<select id="my-select">
<option value=""> Please choose an option </option>
<option value="7">1st@example.com</option>
<option value="8">2nd@example.com</option>
<option value="5">3rd@example.com</option>
<option value="6">4th@example.com</option>
</select>
Relevant part of
myChoiceType
->add('selectUser', ChoiceType::class,
[
'choices' => $this->userChoices,
'choice_value' => function ($choice)
{
return $choice;
},
'choice_label' => function ($value)
{
return $value;
},
'mapped' => false,
'expanded' => false,
'multiple' => false,
]
)
Finally
What am i doing wrong?
How to get ChoiceType display data in html select in the manner i want it to?
Thank you for ideas!
