I have two entities. Entity A is a parent of Entity B. I am creating a REST for entity A which contains entity B as children.
class EntityA {
private $name;
private $type;
private $bs;
}
class EntityB {
private $entityA;
private $color;
}
I basically do a post/put with something like:
{ "name": "anamehere", "type": "atypehere", "bs": [{"color": "blue"}] }
For the important part, the form of Entity A looks something like this:
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('type', TextType::class)
->add('bs', CollectionType::class, array(
'entry_type' => EntityB::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
));
$builder->addEventListener(FormEvents::SUBMIT, array($this, 'onSubmitData'));
}
public function onSubmitData(FormEvent $event) {
// Do Something
}
And my Entity B's form looks something like:
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('color', TextType::class);
$builder->addEventListener(FormEvents::SUBMIT, array($this, 'onSubmitData'));
}
public function onSubmitData(FormEvent $event) {
// Do Something else
}
The problem that I am having is that when I do that POST/PUT the entity B form event onSubmitData is not being called. How can I propagate the event from the EntityA form to the EntityB form. It is working properly in EntityAType
I had the same question as you had, and with a bit of tests I found the answer, so I'm answering here, in case somebody else finds his way here: There IS event propagation for Child form events.
The exact lifecycle is:
Parent PRE_SUBMIT
Child PRE_SUBMIT
GrandChild PRE_SUBMIT
GrandChild SUBMIT
GrandChild POST_SUBMIT
Child SUBMIT
Child POST_SUBMIT
Parent SUBMIT
Parent POST_SUBMIT
About your problem, my guess is that it is caused by the fact you don't use directly EntityB's form in EntityA's form, but a CollectionType of EntityB's form type, and it seems that CollectionType does not naturally dispatch SUBMIT events for each for its children.
Hope this helps anyone!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With