I'm using Symfony Form standalone (i.e. not the full stack framework) to build an "images" type that allows users to upload 1-x images and give each one a title, change the order of the uploaded images and so on.
I've done this by creating an images type which contains a file and a text field. I've also subclassed the CollectionType to act as a holder (like in the cookbook example). Lastly, I have a dedicated storage class associated with the CollectionType which takes care of persisting the uploaded data (I can't persist the individual images separately since I also need to save the order).
This all works more or less, i.e. I can add/update/delete images normally. The only problem I have is when there is a validation error in some other field in the same form (e.g. if I don't fill out a required field). In this situation, the uploaded file never reaches the storage class, and when the form is rendered again, the view data is in the wrong format. So I've added a ViewTransformer to my custom CollectionType that detects this case and performs the necessary transformation. Which basically works, but unfortunately, it puts the transformed data into the wrong place: The FormView instance corresponding to the CollectionType has the correct (i.e. transformed) data, while the child instance (i.e. the image type) has the untransformed values. So the question is: Can I make it pass the transformed data to the correct child, or shouldn't this happen automatically even?
I can provide some example code if it helps, but it might take a moment to extract only the relevant parts. In the meantime I wanted to ask: Am I even approaching this problem in the right way? The documentation on the inner workings of Symfony Form is a bit wanting, so I'm not sure if there wouldn't be an easier way to do what I am trying to do.
EDIT: After a lot of debugging I found out that my problem is that Transformers are called differently during submit()
and setData()
: During submit()
(i.e. when data is loaded from the storage backend), the view transformer runs on the parent (i.e. CollectionType
), and afterwards the converted $viewData
is passed to the children. During submit()
, it's the other way around: First, the child data is mapped, and afterwards the view transformer is run. I think I'll open a ticket on gh...