Symfony form : old values are displayed after saving
I have a symfony form with many checkboxes :
$this->form = new SocialSettingsForm($user);
if ($request->isMethod(sfRequest::POST)) {
$this->form->bind($request->getParameter('social'));
if ($this->form->isValid()) {
$this->form->save();
$this->success = true;
}
}
The problem is the following : after saving the form, the checkboxes act weirdly
If the user checked one, it appears checked after the save (normal behaviour) But if the user unchecked one, it will still appear checked after the save.
I did a var_dump on the form va开发者_运维百科lues, unchecked checkboxes have a NULL value, so i don't understand why they are still checked.
Thanks for your help.
If you use boolean / integer field for saving the checkbox value, use sfValidatorBoolean for your checkboxes. It also converts the input value to a valid boolean. So null will be converted to 0 and will be saved in the database.
Update with workaround
In older versions where this do not work, you can add hidden input with the same name as the checkbox and value of 0, before the checkbox. If the checkbox is not checked, the hidden input value will be sent, otherwise the checkbox value will be sent.
精彩评论