How to set the selected option in the symfony form selection box

I have a form created using Symfony forms.

and in the template I have this selectbox displayed on the page using the render method.

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

Can I set the selected option for this selection box?

Or does it need to be done in the class that creates this form? There is a field creation:

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

yes, it is necessary - you must set the appropriate form value - either through bind() or through the widget default option.

For instance,

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

I hope I answered your question.

+2
source

Source: https://habr.com/ru/post/1286404/


All Articles