doushan7077 2015-04-07 16:24
浏览 28

Symfony表单,带有ResizeformListener和验证的ViewTransformer

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...

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看