简体   繁体   中英

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 :

->add('istrue', null, array())

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.

->add('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

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.

Even if it is a bit tricky, for backward compatibility reasons the 'sonata_type_boolean' will set 1 for yes and 2 for no. If you want to map to a boolean value, just set the option transform to 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

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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