On Symfony 1.4.11 I have a boolean field that is set on a form by a HTML checkbox. When the value is 0 (unchecked) then the HTML of the checkbox is
<input type="checkbox" value="" name="gift_type[valid]">
When I try and save the from it is always saved as zero, I assume because the input has no value.
The code I am using for the checkbox is generated by the symfony admin generator so I thought it would just work “out of the box”. If I uncheck a true value then that works as expected.
schema.yml
GiftType:
columns:
valid: { type: boolean, notnull: true, default: true }
BaseGiftTypeForm.class.php
$this->setWidgets(array(
'valid' => new sfWidgetFormInputCheckbox()
));
$this->setValidators(array(
'valid' => new sfValidatorBoolean(array('required' => false))
));
Your HTML checkbox looks funny,
are you sure it’s been generated by symfony? (the id is missing i.e.)
Anyway all the code you show is OK, there is no need to change the schema type! (ping @richrosa).
sfValidatorBoolean return a boolean value, then it’s passed to the object, and then the object is saved. You have to debug your model. Out of the box, a boolean field in the Doctrine Admin Generator work perfectly fine.
You can add this code in your GiftTypeForm class to help debug:
If you don’t see the “valid” key boolean, there is a validation process error, if it’s here, you have a model issue (have you overwrite the save method?).