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