symfony2中对下拉列表设定默认选中项

控件声明方式:
$form = $this->createFormBuilder($breed)
	->add('species', 'entity', array(
	  'class' => 'BFPEduBundle:Item',
	  'property' => 'name',
	  'query_builder' => function(ItemRepository $er){
		  return $er->createQueryBuilder('i')
					->where("i.type = 'species'")
					->orderBy('i.name', 'ASC');
	  }))
	->add('breed', 'text', array('required'=>true))
	->add('size', 'textarea', array('required' => false))
	->getForm()

----------------------

使用 "data"属性进行赋值:


$form = $this->createFormBuilder()
	->add('status', 'choice', array(
		'choices' => array(
			0 => 'Published',
			1 => 'Draft'
		),
		'data' => 1
	))
	->getForm();



官方介绍: http://symfony.com/doc/2.0/reference/forms/types/field.html


如果“choice”允许多选时,使用 

'data' => array('foo1', 'foo2',...)


参考来源:http://stackoverflow.com/questions/8073236/symfony2-setting-a-default-choice-field-selection


你可能感兴趣的:(symfony2中对下拉列表设定默认选中项)