繁体   English   中英

Sonata_type_boolean错误

[英]Sonata_type_boolean bugs

我现在正在使用Sonata Admin Bundle,在我的模型中,我有一个布尔属性,我想通过以下方式在“编辑”视图中显示:如果属性为true,则为“是”,如果属性为false,则为“ false”。 :

->添加('istrue',null,数组())

如果为true,则显示“ 1”;如果为false,则显示“ 0”。.但是,使用sonata_type_boolean错误,即使该属性为false,也始终显示“是”。

->添加('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

显示“是/否”而不是“是/否”或“真/假:”有点奇怪

我刚刚遇到了同样的问题,并找到了解决方案。

“ sonata_type_boolean”是专用的ChoiceType,其中选项列表被锁定为yes和no。

即使有点棘手,出于向后兼容的原因,“ sonata_type_boolean”也会将1设置为yes,将2设置为no。 如果要映射为布尔值,只需将选项transform设置为true。 例如,当映射到一个布尔值时,您需要这样做。

因此,您应该尝试以下操作:

->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

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM