简体   繁体   English

Sonata_type_boolean错误

[英]Sonata_type_boolean bugs

I'm working right now with sonata admin bundle, in my model I have a Boolean attribute which I want display in my Edit view by : "yes" if the attribute is true, "false" if the attribute is false.. making this : 我现在正在使用Sonata Admin Bundle,在我的模型中,我有一个布尔属性,我想通过以下方式在“编辑”视图中显示:如果属性为true,则为“是”,如果属性为false,则为“ false”。 :

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

displays "1" if true and "0" if false.. but Using the sonata_type_boolean bugs it displays always "yes" even if the attribute is false. 如果为true,则显示“ 1”;如果为false,则显示“ 0”。.但是,使用sonata_type_boolean错误,即使该属性为false,也始终显示“是”。

->add('istrue','sonata_type_boolean', array()) ->添加('istrue','sonata_type_boolean',array())

Any one knows how to fix this ? 有人知道如何解决这个问题吗? Thank you 谢谢

You could try to use a choice type : 您可以尝试使用选择类型:

->add('istrue', 'choice', array(
    'choices' => array(
        0 => 'False',
        1 => 'Yes'
    )
))

Documentation : https://sonata-project.org/bundles/admin/master/doc/reference/field_types.html#choice 文档: https : //sonata-project.org/bundles/admin/master/doc/reference/field_types.html#choice

A bit weird to display Yes / False instead of Yes / No or True / False :) 显示“是/否”而不是“是/否”或“真/假:”有点奇怪

I have just had the same problem, and found the solution. 我刚刚遇到了同样的问题,并找到了解决方案。

The 'sonata_type_boolean' is a specialized ChoiceType, where the list of choices is locked to yes and no. “ sonata_type_boolean”是专用的ChoiceType,其中选项列表被锁定为yes和no。

Even if it is a bit tricky, for backward compatibility reasons the 'sonata_type_boolean' will set 1 for yes and 2 for no. 即使有点棘手,出于向后兼容的原因,“ sonata_type_boolean”也会将1设置为yes,将2设置为no。 If you want to map to a boolean value, just set the option transform to true. 如果要映射为布尔值,只需将选项transform设置为true。 For instance, you need to do so when mapping to a doctrine boolean. 例如,当映射到一个布尔值时,您需要这样做。

So you should try with this: 因此,您应该尝试以下操作:

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

You can find more info here: https://sonata-project.org/bundles/core/master/doc/reference/form_types.html 您可以在这里找到更多信息: https : //sonata-project.org/bundles/core/master/doc/reference/form_types.html

Hope this helps! 希望这可以帮助!

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

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