From 5b83d183b5a5eb465d87f21eb26346029e14743c Mon Sep 17 00:00:00 2001 From: sarah-eit Date: Fri, 19 Jan 2024 10:42:15 +0100 Subject: [PATCH] [Form collections] Add informations about the form events --- form/form_collections.rst | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/form/form_collections.rst b/form/form_collections.rst index a2726ed1ed6..8811e532d0c 100644 --- a/form/form_collections.rst +++ b/form/form_collections.rst @@ -125,6 +125,48 @@ inside the task form itself:: } } +.. note:: + + If you want to access the linked data, you will need to use events:: + + // src/Form/TagType.php + + // ... + public function buildForm(FormBuilderInterface $builder, array $options): void + { + // ... + $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) { + $data = $event->getData(); + // ... + }); + } + // ... + + And when the form (which call the CollectionType) sets the ``by_reference`` option + to false, the entire form must to be inside a ``PRE_SET_DATA`` event:: + + // src/Form/TaskType.php + + // ... + public function buildForm(FormBuilderInterface $builder, array $options): void + { + // ... + $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) { + $form = $event->getForm(); + $form->add('tags', CollectionType::class, [ + 'entry_type' => TagType::class, + 'by_reference' => false, + ]); + }); + // ... + } + // ... + +.. seealso:: + + If you want to learn more about the form events, read :ref:`events` + and :ref:`dynamic_form_modification`. + In your controller, you'll create a new form from the ``TaskType``:: // src/Controller/TaskController.php