0
加入收藏让我先解释一下什么是我想要做的事。的Symfony2式事件侦听
所以我认为有一个名为类型字段实体,称为属性(也可以是文本,电子邮件,或multi_option)和被叫的PropertyValue另一个实体,这些实体的财产
的价值所以我发现this tutorial和我问题是在EventListener中,而不是一个简单的字段,我怎样才能添加一个选择或复选框的其他实体的值?
这里有我的事件监听的代码,你可以看到我面临的问题
<?php
namespace Comehoy\AdBundle\Form\EventListener;
use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;
class AddValueFieldSubscriber implements EventSubscriberInterface
{
private $factory;
public function __construct(FormFactoryInterface $factory)
{
$this->factory = $factory;
}
public static function getSubscribedEvents()
{
// Tells the dispatcher that we want to listen on the form.pre_set_data
// event and that the preSetData method should be called.
return array(FormEvents::PRE_SET_DATA => 'preSetData');
}
public function preSetData(DataEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
// During form creation setData() is called with null as an argument
// by the FormBuilder constructor. We're only concerned with when
// setData is called with an actual Entity object in it (whether new,
// or fetched with Doctrine). This if statement let's us skip right
// over the null condition.
if (null === $data) {
return;
}
// check if the ProprertyValue object is "new"
$type = $data->getI18nField()->getProperty()->getType();
if ('multi_option' === substr($type, 0, 12)) {
/*
*
* Here is the problem since I'm kind of sure this is not the way to do this
*
*/
$builder = $this->factory->createNamedBuilder('entity', 'value');
$builder->add('value', 'entity', array(
'class' => 'ComehoyAdBundle:Translation\AdPropertyOption',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('po')
->orderBy('po.value', 'ASC');
}
));
} else {
//It's not a multi option field so we use the type directly
$form->add($this->factory->createNamed($type, 'value'));
}
}
}
那么我基本上试图做的是,我可以在buildForm使用做一个类型的同样的事情$工具的详情参数
感谢
所以基本上我错过了一个参数吗?感谢您的回答 –
您已经创建了构建器,并且没有添加由构建器创建的表单,以便让侦听器连接到表单。 –