I have my own FormType that needs to be added to the parent Entity when the parent form is saved.
In Symfony <2.3, you can do this by doing the following:
class FooType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { parent::buildForm($builder, $options); ... $builder->getParent()->addEventSubscriber(new FooSubscriber) } } class FooSubscriber implements EventSubscriberInterface { static function getSubscribedEvents() { return array( FormEvents::POST_SUBMIT => 'postSubmit' ); } }
But after migrating to Symfony 2.6, I found that $builder->getParent() was removed. But now I can’t listen to the submitted parent.
So, I added a listener to my builder and referred to the parent from the Subscriber. But actually this does not work, since I check the parent form as valid, which it is not, since it has not yet been submitted:
function postSubmit(FormEvent $e) { if ($e->getForm()->getParent()->getRoot()->isValid()) {
This false is caused by the following piece of code:
// Symfony\Component\Form\Form.php @ line 744 public function isValid() { if (!$this->submitted) { return false; }
And since the parent form first iterates over all the children and submits it, before setting $this->submitted = true to itself ... I'm not sure if the parent is valid.
TL DR
How to add Eventenerener to the parent form without the need to adjust the parent form? I want my FooType to be something that I can add to all forms without knowing / don't remember, to make some logic for this particular FooType.
Oskar source share