在 Symfony v4.2 中将带有数据的数组传递给 ChoiceType (html select)
Pass array with data to ChoiceType (html select) in Symfony v4.2
简介
对于我的个人项目,我使用
- Symfony v4.2
- PHP 7.2.12
问题
我不知道如何让 ChoiceType
元素显示 IDs
和 EMAILs
作为 HTML SELECT
选项 values 和索引.
例子
图片显示 html select
的当前状态以及来自数据库的数据(id
和 email
)。
代码
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,
]
)
终于
我做错了什么?
如何让 ChoiceType
以我想要的方式在 html select
中显示 data
?
谢谢你的想法!
只需将其作为一维数组传递,将电子邮件作为键,将 ID 作为值。
$choices = [
'1st@example.com' => 7,
'2nd@example.com' => 8,
];
根据 the docs,choices
值必须是一个简单的键 => 值数组。
The choices option is an array, where the array key is
the item's label and the array value is the item's value
您可以使用以下代码轻松获得所需的选择数组:
$choices = [];
foreach ($this->userChoices as $choice) {
$choices[$choice['email']] = $choice['id'];
}
然后使用下面的初始化代码:
->add('selectUser', ChoiceType::class,
[
'choices' => $choices,
'mapped' => false,
'expanded' => false,
'multiple' => false
]
)
并不是说我删除了 choice_value
和 choice_label
,它们在这种情况下没有任何作用。
简介
对于我的个人项目,我使用
- Symfony v4.2
- PHP 7.2.12
问题
我不知道如何让 ChoiceType
元素显示 IDs
和 EMAILs
作为 HTML SELECT
选项 values 和索引.
例子
图片显示 html select
的当前状态以及来自数据库的数据(id
和 email
)。
代码
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,
]
)
终于
我做错了什么?
如何让 ChoiceType
以我想要的方式在 html select
中显示 data
?
谢谢你的想法!
只需将其作为一维数组传递,将电子邮件作为键,将 ID 作为值。
$choices = [
'1st@example.com' => 7,
'2nd@example.com' => 8,
];
根据 the docs,choices
值必须是一个简单的键 => 值数组。
The choices option is an array, where the array key is the item's label and the array value is the item's value
您可以使用以下代码轻松获得所需的选择数组:
$choices = [];
foreach ($this->userChoices as $choice) {
$choices[$choice['email']] = $choice['id'];
}
然后使用下面的初始化代码:
->add('selectUser', ChoiceType::class,
[
'choices' => $choices,
'mapped' => false,
'expanded' => false,
'multiple' => false
]
)
并不是说我删除了 choice_value
和 choice_label
,它们在这种情况下没有任何作用。