Closed
Description
I'm trying to setup a dynamic form collection using events based on the documentation.
First I add the collection to the form:
<?php
$builder->add('properties', 'collection', array(
'type' => new Property(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'label' => 'Properties',
));
Then in the Property form, I add an event subscriber:
<?php
$subscriber = new AddDynamicFieldsSubscriber($builder->getFormFactory());
$builder->addEventSubscriber($subscriber);
Then my subscriber class:
<?php
class AddDynamicFieldsSubscriber implements EventSubscriberInterface
{
private $factory;
public function __construct(FormFactoryInterface $factory)
{
$this->factory = $factory;
}
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_SET_DATA => 'preSetData');
}
public function preSetData(DataEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data || false === $data->needsDynamicFields()) {
return;
}
$form->add($this->factory->createNamed('choice', 'test', null, array(
'choices' => array(1, 2, 3,),
'expanded' => false,
'label' => 'Test',
'empty_value' => 'Select...',
)));
}
}
The form view from $form->createView()
contains the elements that were added by the event subscriber. The prototype form view from form.properties.get('prototype') does not contain the elements elements that were added by the event subscriber.
Is there another supported method of getting an updated prototype in this scenario? If not - what would be the best way to customize to get this working?