tags:

views:

943

answers:

1

I have a form created with Symfony forms.

and in the template i have this selectbox, displayed on the page with the render method.

<?php echo $form['field']->render() ?>

is it possible to set the selected option of this select box?

Or does this have to be done in the class that creates this form? There is the creation of the field done:

public function configure() {
    $this->widgetSchema['field'] = new sfWidgetFormSelect(array("choices" => array('1' => 'test1','2' => 'test2')));
  }
A: 

Hi.

yes, sure — you should have set corresponding form value — either via bind(), either via widget's default option.

For example,

public function configure() 
{
    $this->widgetSchema['field'] = new sfWidgetFormSelect(array(
        "choices" => array('1' => 'test1','2' => 'test2'), 
        'default' => 2));
}

Hope I've answered your question.

develop7
this doesn't seem to work... I read about it, but the first choice 1 => test1 always gets selected. I get no error, but the selected option isn't inserted.
Kennethvr
it seemed that i shouldn't use the 1 and 2 in the array as keys because the database used other values in the enum field.as soon as the the values where the same, symfony did it correctly.
Kennethvr