我有一个公告实体,EditAnnouncementType
表单被映射到该公告实体。我还有另外两个CheckBoxType
表单可以自动更新各自的字段,但是ChoiceType
表单不起作用。
$builder
->add('edit', SubmitType::class,
array
(
'label' => 'Save changes',
'attr' => ['class' => 'btn btn-primary']
))
->add('type', ChoiceType::class,
[
'choices' => [
'info_type' => 1,
'star_type' => 2,
'alert_type' => 3,
'lightbulb_type' => 3,
'event_type' => 4,
'statement_type' => 5,
'cat_type' => 6,
'hands_type' => 7
],
'mapped' => true,
'expanded' => true,
'required' => true,
])
“公告”实体和类型字段
class Announcement
{
/**
* @var int
*/
private $type;
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* @param int $type
*
* @return $this
*/
public function setType(int $type)
{
$this->type = $type;
return $this;
}
答案 0 :(得分:1)
我怀疑Symfony会以某种方式严格检查值(使用===
)。
而且由于您的getter返回了string
,因此映射无法正确进行。
您应该尝试修复吸气剂:
/**
* @return int
*/
public function getType(): int
{
return $this->type;
}
还请注意,您的选择数组可能有问题:
// be careful: those two have the same value
'alert_type' => 3,
'lightbulb_type' => 3,
这肯定会给Symfony造成问题,特别是如果它使用array_values
以便从实体的价值中选择正确的选择时,尤其如此。