我现在正在使用sonata admin bundle,在我的模型中我有一个布尔属性,我希望在我的编辑视图中显示:“yes”如果属性为true,如果属性为false则为“false”。 :
- > add('istrue',null,array())
如果为true则显示“1”,如果为false则显示“0”..但是使用sonata_type_boolean错误,即使属性为false,它也始终显示“是”。
- > add('istrue','sonata_type_boolean',array())
任何人都知道如何解决这个问题?谢谢
您可以尝试使用选择类型:
->add('istrue', 'choice', array(
'choices' => array(
0 => 'False',
1 => 'Yes'
)
))
文档:https://sonata-project.org/bundles/admin/master/doc/reference/field_types.html#choice
显示Yes / False而不是Yes / No或True / False有点奇怪:)
我刚遇到同样的问题,并找到了解决方案。
'sonata_type_boolean'是一个专门的ChoiceType,其中选择列表被锁定为yes和no。
即使它有点棘手,出于向后兼容性的原因,'sonata_type_boolean'将为1设置为1,为no设置为2。如果要映射到布尔值,只需将选项transform设置为true。例如,在映射到doctrine boolean时需要这样做。
所以你应该尝试这个:
->add('istrue','sonata_type_boolean', array(
'label' => '<Your label here if any>',
// the transform option enable compatibility with the boolean field (default 1=true, 2=false)
// with transform set to true 0=false, 1=true
'transform' => true,
'required' => true
))
你可以在这里找到更多信息:https://sonata-project.org/bundles/core/master/doc/reference/form_types.html
希望这可以帮助!