Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select multiple symfony checkboxes when editing object

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:

Default checkbox setup

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:

Edit checkbox setup

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?

like image 723
Patrick Avatar asked May 17 '26 06:05

Patrick


1 Answers

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).

like image 58
Intru Avatar answered May 20 '26 20:05

Intru