如何重用 Symfony2 中遗漏的某些字段的表单
我正在使用嵌入式表单来制作注册表单,其中包含多个相关实体的字段。正如我在此处的问题中向我解释的:
Symfony2 表单中的数据对象与需要填写的内容不完全匹配
这工作得很好。但是假设我想重新使用我嵌入的表单之一,但保留一些字段。
那么我的选择是什么?
- 我是否要创建一个额外的 formType 来扩展原始 formType?
- 我是否决定在视图中省略某些字段?
但在我看来,这并不像是一个视图决定。对我来说,针对每个不同的用例进行扩展感觉像是一种糟糕的重用实践。
其他人是如何解决这个问题的?
谢谢,
迪特尔
I am using embedded forms to be able to make a registration form, which holds fields of several related entities. As explained to me in my question over here:
Symfony2 form where the data objects doesn't match exactly what needs to be filled in
This works just fine. But say I want to re-use one of the forms that I embedded, but leave some of the fields out.
Then what are my options?
- Do I create an extra formType which extends the original one?
- Do I decide in the view to leave some fields out?
This doesn't feel like a view decision in my opinion though. And extending for each different use case feels like a bad re-use practice to me.
How are other people solving this?
Thanks,
Dieter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最好的解决方案是创建一个基本的抽象类型并让您的类型扩展它。这是一个简短的示例:
现在您可以扩展它并包含缺少的字段
I think the best solution is to create a base abstract Type and let your Types extend it. Here's a short example:
Now you can just extend it an include the missing fields
请参阅此github拉取请求: https://github.com/symfony/symfony-docs/pull /765
这似乎正是您正在寻找的东西。
see this github pull request: https://github.com/symfony/symfony-docs/pull/765
it seems to be exactly what you are looking for.