I have a form with 3 dependent fields:
Manufacturer → ManufacturerProductGroup → ManufacturerProductSeries
So, I want to choose a manufacturer based on the ProductGroup manufacturer and based on the ProductGroup product series.
There is a CookBook entry on how to deal with such dynamic forms; it works easily for ManufacturerProductGroup. The problem is that ManufacturerProductSeries is dependent on a different dynamic shape / field.
The problem is that I cannot add the Listener to the closure, since I only have access to FormInterface no longer to FormBuilderInterface.
I can’t do this with only one listener, as in the PRE_SET_DATA listener, since the form will be sent next, which will prevent any changes. I need a way to add a listener dynamically or perhaps update only the data in the POST_SUBMIT listener and not replace the entire field / form.
This is the code that gives the error, as I add a listener to a form field that does not exist (producerProductGroup is available only after the pre_set_data event)
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('manufacturer', 'entity', array('class' => 'ArticleBundle:manufacturer', 'empty_value' => false));
$manufacturerProductGroupModifier = function(FormInterface $form, Manufacturer $manufacturer)
{
$manufacturerProductGroups = $this->em->getRepository('ArticleBundle:ManufacturerProductGroup')
->findAvailableByManufacturer($manufacturer);
$form->add(
'manufacturerProductGroup',
'entity',
array(
'class' => 'ArticleBundle:ManufacturerProductGroup',
'empty_value' => '',
'choices' => $manufacturerProductGroups
)
);
};
$manufacturerProductSeriesModifier = function(FormInterface $form, ManufacturerProductGroup $manufacturerProductGroup)
{
$manufacturerProductSeries = $this->em->getRepository('ArticleBundle:ManufacturerProductSeries')
->findAvailableByManufacturerProductGroup($manufacturerProductGroup);
$form->add(
'manufacturerProductSeries',
'entity',
array(
'class' => 'ArticleBundle:ManufacturerProductSeries',
'empty_value' => '',
'choices' => $manufacturerProductSeries
)
);
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function(FormEvent $event) use ($manufacturerProductGroupModifier, $manufacturerProductSeriesModifier) {
$article = $event->getData();
$manufacturerProductGroupModifier($event->getForm(), $article->getManufacturer());
$manufacturerProductSeriesModifier($event->getForm(), $article->getManufacturerProductGroup());
}
);
$builder->get('manufacturer')->addEventListener(
FormEvents::POST_SUBMIT,
function(FormEvent $event) use ($manufacturerProductGroupModifier) {
$manufacturer = $event->getForm()->getData();
$manufacturerProductGroupModifier($event->getForm()->getParent(), $manufacturer);
}
);
$builder->get('manufacturerProductGroup')->addEventListener(
FormEvents::POST_SUBMIT,
function(FormEvent $event) use ($manufacturerProductSeriesModifier) {
$manufacturerProductGroup = $event->getForm()->getData();
$manufacturerProductSeriesModifier($event->getForm()->getParent(), $manufacturerProductGroup);
}
);
}