views:

312

answers:

2

Using Doctrine in symfony 1.4 I have several boolean columns defined (stored as a tinyint in mySQL). The checkbox widgets are always rendering as checked, even when the returned value is '0'. It seems related to this ticket.

Is this a common problem? Is there a workaround?

I can get it working by changing line 70 in sfWidgetFormInputCheckbox to:

if (null !== $value && $value !== false && $value !== 0)

but I'd rather not alter core symfony files.

A: 

Not sure in what context you're seeing the problem. I can get the following to work fine:

$database_field = '1;0;1;0;1;0;1';
$checked = explode(';', $database_field);
$this->form->getWidget('a_set_of_checkboxes')->setDefault($checked);

... the checkboxes with 1's get checked, 0's not.

I'm using "sfWidgetFormChoice" widgets with the attributes "multiple: true" and "expanded: true" so they're not individual checkbox widgets, but rather a single widget that contains an array of checkboxes/values.

Tom
My widget class is sfWidgetFormInputCheckbox because it's just a single field.
gruner
A: 

Symfony unresolved bug, see http://www.devexp.eu/2009/04/23/sfwidgetforminputcheckbox-unchecked-bug/

Your database returns a 0 for a unchecked box but Symfony renders it as checked.

You can patch yourself by editing symfony/lib/widget/sfwidgetforminputcheckbox.class, line 70 (in version 1.4.6), change:

if (null !== $value && $value !== false)

in

if (null !== $value && $value !== false && $value != 0)

chrizzler