I have a symfony 1.4 application and in a form I am using sfWidgetFormChoice to create multiple checkboxes.
I am able to set the defaults up without a problem, but after saving the object and viewing the edit form, I cannot get the checkboxes to be checked.
The values from the 'checked' checkboxes are imploded and saved into a single field.
For instance:

This is the default setup.
This saves into the database as Full-Time;Hourly.
That works great.
When I am editing this object, the form looks like this:

The setDefault function doesn't work when editing an object (because there is 'data' there, so we don't need the default).
How I'm creating the field object:
$choices = array(
'Full-Time' => 'Full-Time',
'Part-Time' => 'Part-Time',
'Hourly' => 'Hourly',
'Contract' => 'Contract'
);
$this->widgetSchema['emp_type'] = new sfWidgetFormChoice(
array(
'choices' => $choices,
'multiple' => true,
'expanded' => true
),
array()
);
$this->setDefault('emp_type', array('Full-Time', 'Hourly'));
How can I set the appropriate checkboxes to be checked when editing an object?
sfWidgetFormChoice::render() expects that the $value argument is an array. So I think there are two ways to achieve what you want:
Create a new Widget class that inherits from sfWidgetFormChoice. Only implement the render method:
public function render($name, $value = null, $attributes = array(), $errors = array()) {
$value = explode(';', $value);
return parent::render($name, $value, $attributes, $errors);
}
Or you can adjust the caller of the render method or your model so that sfWidgetFormChoice::render() gets called with $value as an array (put the explode somewhere else).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With