【发布时间】:2015-06-19 07:50:06
【问题描述】:
如何使用外部数据数组填充选项选项?
$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
;
}
....
}
这是正确的解决方案吗?
【问题讨论】:
-
你在使用 EventListeners 吗?
-
要么这样,要么将您的表单更改为服务,并通过服务注入依赖关系并使用构造函数。 symfony.com/fr/doc/current/reference/…
-
或者通过控制器传入。