From 0feac63ce10524dc730169c827300a9ee68cf1b9 Mon Sep 17 00:00:00 2001 From: Gregor Nathanael Meyer Date: Tue, 7 Sep 2021 09:33:31 +0200 Subject: [PATCH] Inserted a warning about a limitation model transformers There is a surprising limitation of model transformers, that cannot filter out items for a Collection field type. Took me a whole day to find out, so here is a warning about that. See https://github.com/symfony/symfony/issues/36469 for details. (Maybe that issue should be linked for further details?). --- form/data_transformers.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/form/data_transformers.rst b/form/data_transformers.rst index c21976dadce..08479ff16a5 100644 --- a/form/data_transformers.rst +++ b/form/data_transformers.rst @@ -482,6 +482,21 @@ Which transformer you need depends on your situation. To use the view transformer, call ``addViewTransformer()``. +.. caution:: + + Be careful with model transformers and + :doc:`Collection ` field types: As the + Collection's children are created early at `PRE_SET_DATA` by its + `ResizeFormListener`, their data is populated later from the Norm Data. So + your model transformer cannot reduce the number of items within the + Collection (i.e. filtering out some items), as in that case the collection + ends up with some empty children. + + A possible workaround for that limitation could be not using the underlying + object directly, but a DTO (Data Transfer Object) instead, that implements + the transformation of such incompatible data structures. + + So why Use the Model Transformer? ---------------------------------