3

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! worked like a charm. BTW, there is a minor error in your answer's code, the parent::render() has two renders

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.