Description
Symfony version(s) affected: 5.0.7
Description
I have a CollectionType form field collecting a custom Type. The data structure from the model doesn't match the way the form should work, so I use a ModelTransformer to transform the data, reducing the count of items. This works fine, I can see that the field has the correct data structure in its "Normalized Format". But the collection now still has the count of items from the model data, while the transformed data is merged into that, leading to several empty items. I expected the collection to work (only) with the "Normalized Format" but in fact I get the count of items from the "Model Format" and the "Normalized Format" merged into them.
How to reproduce
Have a CollectionType form field and add a ModelTransformer that reduces the count of items from n to m. Then observe the form to render n items, with the first m having the transformed data and the other having null.
Possible Solution
I could switch the whole Form to use a DTO and transform the data there (a good idea anyway), but at first I want to have an opinion if this is a bug in the Form component.
Additional context
In my case, I see this data in the "Model Format" section of the profiler:
[▼
[▼
"month" => "01"
"value" => 202001
"fromYear" => "2020"
]
[▼
"month" => "02"
"value" => 202002
"fromYear" => "2020"
]
[▼
"month" => "03"
"value" => 202003
"fromYear" => "2020"
]
[▼
"month" => "04"
"value" => 202004
"fromYear" => "2020"
]
[▼
"month" => "05"
"value" => 202005
"fromYear" => "2020"
]
[▼
"month" => "06"
"value" => 202006
"fromYear" => "2020"
]
[▼
"month" => "07"
"value" => 202007
"fromYear" => "2020"
]
[▼
"month" => "08"
"value" => 202008
"fromYear" => "2020"
]
[▼
"month" => "09"
"value" => 202009
"fromYear" => "2020"
]
[▼
"month" => "10"
"value" => 202010
"fromYear" => "2020"
]
[▼
"month" => "11"
"value" => 202011
"fromYear" => "2020"
]
[▼
"month" => "12"
"value" => 201912
"fromYear" => "2019"
]
]
And this (expected) data in "Normalized Data" (that is what my transformer should do, as the custom type expects data for those 13 fields):
[▼
[▼
"fromYear" => "2019"
"value01" => null
"value02" => null
"value04" => null
"value05" => null
"value06" => null
"value07" => null
"value08" => null
"value09" => null
"value10" => null
"value11" => null
"value12" => 201912
]
[▼
"fromYear" => "2020"
"value01" => 202001
"value02" => 202002
"value03" => 202003
"value04" => 202004
"value05" => 202005
"value06" => 202006
"value07" => 202007
"value08" => 202008
"value09" => 202009
"value10" => 202010
"value11" => 202011
"value12" => null
]
]
But the collection has 12 children instead of the expected 2 inclined by the Normalized Format:
Child 0 Normalized Format:
[▼
"fromYear" => "2019"
"value01" => null
"value02" => null
"value04" => null
"value05" => null
"value06" => null
"value07" => null
"value08" => null
"value09" => null
"value10" => null
"value11" => null
"value12" => 201912
]
Child 1 Normalized Format:
[▼
"fromYear" => "2020"
"value01" => 202001
"value02" => 202002
"value03" => 202003
"value04" => 202004
"value05" => 202005
"value06" => 202006
"value07" => 202007
"value08" => 202008
"value09" => 202009
"value10" => 202010
"value11" => 202011
"value12" => null
]
Child 2-11 Normalized Format:
null