Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions form/form_collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down