确定在控制器中选择了哪种 Radio Type 形式
Determine which RadioType form was selected in contorller
我有一个表单,其中包含多个 RadioType 来表示公告实体的类别。
$builder
->add('info', RadioType::class,
[
'label_attr' => ['class' => 'sr-only'],
'required' => false
])
->add('star', RadioType::class,
[
'label_attr' => ['class' => 'sr-only'],
'required' => false
])
我计划为 8 个选择设置 8 个不同的 RadioType。确定选择了哪种 RadioType 的最佳方法是什么。我当前的实现是为每个语句添加一个 if
语句,但这似乎是一个糟糕的解决方案。
if ($form->getData()['info'] == true) {
//do stuff
}
if ($form->getData()['star'] == true) {
//do stuff
}
According to the documentation, you shouldn't typically be using RadioType
directly. Using the ChoiceType
object 允许您遵循预期的 HTML 标准,这意味着您对每个元素使用相同的名称但不同的值。这样,浏览器将像单选按钮一样,自动将用户限制为单一选择。
<?php
$builder->add('yourCategory', ChoiceType::class, [
'choices' => [
'Info' => 'info',
'Star' => 'star',
'Some other label' => 'other',
],
// attributes for label elements
'label_attr' => ['class' => 'sr-only'],
// attributes for input elements
'choice_attr' => [
'Info' => ['class' => 'fa fa-info'],
'Star' => ['class' => 'fa fa-star'],
'Some other label' => ['class' => 'whatever'],
],
// setting these options results in radio buttons
// being generated, instead of a select element
'expanded' => true,
'multiple' => false,
]);
然后在你的控制器中:
switch($form->getData()['yourCategory']) {
case 'info':
// do stuff
break;
case 'star':
// do stuff
break;
case 'other':
// do stuff
break;
}
所以我最终能够手动更改每个选项的标签
{# Manually set the label for each choice in the form, so only an icon is shown #}
<label for="{{ form.choices.children[0].vars.id}}" class="mt-2">
<span class="fa-stack fa-2x type-icon" id="info-type">
<span class="fas fa-circle fa-stack-2x circle"></span>
<span class="info fas fa-info fa-stack-1x"></span>
</span>
</label>
我有一个表单,其中包含多个 RadioType 来表示公告实体的类别。
$builder
->add('info', RadioType::class,
[
'label_attr' => ['class' => 'sr-only'],
'required' => false
])
->add('star', RadioType::class,
[
'label_attr' => ['class' => 'sr-only'],
'required' => false
])
我计划为 8 个选择设置 8 个不同的 RadioType。确定选择了哪种 RadioType 的最佳方法是什么。我当前的实现是为每个语句添加一个 if
语句,但这似乎是一个糟糕的解决方案。
if ($form->getData()['info'] == true) {
//do stuff
}
if ($form->getData()['star'] == true) {
//do stuff
}
According to the documentation, you shouldn't typically be using RadioType
directly. Using the ChoiceType
object 允许您遵循预期的 HTML 标准,这意味着您对每个元素使用相同的名称但不同的值。这样,浏览器将像单选按钮一样,自动将用户限制为单一选择。
<?php
$builder->add('yourCategory', ChoiceType::class, [
'choices' => [
'Info' => 'info',
'Star' => 'star',
'Some other label' => 'other',
],
// attributes for label elements
'label_attr' => ['class' => 'sr-only'],
// attributes for input elements
'choice_attr' => [
'Info' => ['class' => 'fa fa-info'],
'Star' => ['class' => 'fa fa-star'],
'Some other label' => ['class' => 'whatever'],
],
// setting these options results in radio buttons
// being generated, instead of a select element
'expanded' => true,
'multiple' => false,
]);
然后在你的控制器中:
switch($form->getData()['yourCategory']) {
case 'info':
// do stuff
break;
case 'star':
// do stuff
break;
case 'other':
// do stuff
break;
}
所以我最终能够手动更改每个选项的标签
{# Manually set the label for each choice in the form, so only an icon is shown #}
<label for="{{ form.choices.children[0].vars.id}}" class="mt-2">
<span class="fa-stack fa-2x type-icon" id="info-type">
<span class="fas fa-circle fa-stack-2x circle"></span>
<span class="info fas fa-info fa-stack-1x"></span>
</span>
</label>