Skip to content

[Form] [Form collections] Add informations about the form events #19443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 5.4
Choose a base branch
from
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