Search code examples
oracle-databasesymfony1widget

sfWidgetFormInputCheckbox do not work properly


I have a problem with a sfWidgetFormInputCheckbox. It do not save false in the database when the checkbox is unchecked.

In my schame my field is a boolean, and i don't have bug in the value displayed.

Set the default value like this : $this->setDefault('status', false); don't work either.

My widget :

$this->widgetSchema['SUSPENSION_TEMP'] = new sfWidgetFormInputCheckbox();
$this->validatorSchema['SUSPENSION_TEMP'] = new sfValidatorBoolean(array('required' => false));

The default :

$this->setDefault('SUSPENSION_TEMP', false);

Any ideas ?

Edit:

It save 1 when the checkbox is checked. And do not change the database value when you unchecked and save. So once you checked once, value is always 1 in database (true for my code).

Edit2:

I have two clues to add. If the field is an integer in the schema.xml, both values work, bu we got a poblem on value displayed (checkbox checked qih the 0 value). Here, he field is a boolean.

The setter receive two kinds of value, 'on' when checkbox is checked and true when it's not. I tried to set default value to false in form, and to overide he setter. Both don't work.

I mean the setter work, but for an unknown reason the basic seer is called right after with a wrong value.

Edit3:

Ok, i understand part of the problem. The setter is not called, when the checkbox is unchecked. I thought it was because, m code displayed a var_dump(). It was because later i copy the old version of this object in archive purpose.

But symfony should detect that the field has been rendered and it doesn't. I don't know why. I know why in html, but obviously symfony should be able to tell himself "hey, i add a checkbox here".

Is there an elegant way o do this, or should i checked it by hand ?


Solution

  • The problem came from my form handling. My legacy code, did no render all fields (ex : some stamp field, updated by a behavior). It was using fromArray, then save. This update only the fiels rendered.

    The traditionnal symfony form, give null to all non-rendered fiels in order to chcuk for required validation.

    So you need to check by yourself and set to 0, when you are using a fromArray then save. Still, i find the form->save really dumb, because you need to send useless(in this case) or confidentials data to your client in order to use it.