Let me first explain what’s I’m trying to do.
So I have a entity called property that have a field called type(it can be text, email, or multi_option) and another entity called propertyValue which is the value of the property
So I found this tutorial and my question is in the EventListener instead of a simple field how can I add a select or set of checkbox having the values of other entity?
Here you have my EventListener code and you can see where I’ facing problem
<?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'));
}
}
}
So what I’m basically try to do is to the same thing that I can do in buildForm of a type using $bulder parameter
Thanks
If you are using SF 2.1 then: