为什么在事件订阅者事件中使用FormFactory?

时间:2014-04-25 23:56:39

标签: symfony

为什么人们在事件订阅者事件中使用FormFactory?

我目前有一个像这样的活动订阅者......

public static function getSubscribedEvents()
{
    return array(
        FormEvent::PRE_SET_DATA => 'doSomething',
    );
}

public function doSomething(FormEvent $event)
{
    $form = $event->getForm();

    ... /// other stuff, see below /// ...
}

目前我正在使用..

    $form->add('field', 'type', options);

但是很多其他开发人员都使用表单工厂..

    $form->add($formFactory->createNamed('field', 'type', null, options);

两者似乎都有用,所以使用额外表单工厂对象的原因是什么?

1 个答案:

答案 0 :(得分:0)

v2.2起,您才能使用->add(name,type...)。因此,为旧版本编写的代码应使用$formFactory来构建FormInterface的实例