如何使用外部数据阵列填充选项选项?
$builder->add('gender', 'choice', array(
'choices' => [array from webservice or other class]
));
文档仅显示简单的静态数组 http://symfony.com/fr/doc/2.7/reference/forms/types/choice.html#choices
我试过了:
控制器:
$event = new Event();
$form = $this->createForm(new EventType($this->get('api')), $event);
表格:
class EventType extends AbstractType
{
protected $myservice;
public function __construct($myservice)
{
$this->myservice = $myservice;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('titre')
->add('id_rubrique', 'choice', array(
'choices' => $this->myservice->getRubriques
;
}
....
}
这是正确的解决方案吗?
答案 0 :(得分:2)
将表单定义为服务:
services:
my.form:
class: EventType
arguments:
- @api
然后在控制器中:
$event = new Event();
$form = $this->createForm('my.form', $event);
答案 1 :(得分:0)
例如
->add('education', 'choice', array('label' => 'Education *','choices' => $this->getEducation(), 'required' => true, 'empty_value' => 'select', 'empty_data' => null))
$ this-> getEducation()是数组
// Array('option1Value'=>'option1Name'...