Reputation: 675
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
Upvotes: 0
Views: 1014
Reputation: 11
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!
Upvotes: 1
Reputation: 2206
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 :)
Upvotes: 3