how to set the selected option in Symfony forms select box

◇◆丶佛笑我妖孽 提交于 2019-12-22 08:46:49

问题


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')
      )
    );
  }

回答1:


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.



来源:https://stackoverflow.com/questions/1011586/how-to-set-the-selected-option-in-symfony-forms-select-box

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!