简体   繁体   English

根据Symfony 1.4 / Doctrine 1.2中的数据库查询创建唯一的表单字段

[英]Create unique form fields based on database query in Symfony 1.4/Doctrine 1.2

I have this wild project happening at work, and we happen to use Symfony 1.4 so I was hoping, ideally, to keep this project within the framework's capabilities as much as possible. 我正在执行一个疯狂的项目,我们碰巧使用了Symfony 1.4,因此,我希望在理想情况下,尽可能将这个项目保持在框架的功能范围内。 Unfortunately, despite quite a bit of research and searching, I haven't yet found a way to generate sfWidgetForm elements based on queries to the database. 不幸的是,尽管进行了大量研究和搜索,但我还没有找到一种基于对数据库查询生成sfWidgetForm元素的方法。

I'll outline what the purpose of this is by explaining the flow of the project. 我将通过解释项目流程来概述其目的。

  1. The user submits a form which contains most of the data our advertisers need. 用户提交的表单包含我们的广告客户所需的大多数数据。
  2. Based on this data, rows from a database can be called. 基于此数据,可以调用数据库中的行。 Perhaps this form was filled, and in it, the user claims to be interested in gardening. 也许此表格已填写,并且用户声称对园艺感兴趣。 Based on that relevance this will yield a row (or several rows, but whatever) of form fields related to gardening. 基于此相关性,这将产生一行与园艺相关的表单字段(或几行,但无论如何)。
  3. Here in lies the important part. 这是重要的部分。 This gardening related content is essentially an outline of form fields required to a complete a submission for someone who is interested in gardening. 与园艺相关的内容实质上是填写表单的概述,这些表单对于对园艺感兴趣的人完成提交。 We will get form element types, names, labels, requirement status, enablement status, etc. Based on the ids of these elements, we can see if any of them are dependent on each other from another table. 我们将获得表单元素的类型,名称,标签,需求状态,启用状态等。基于这些元素的ID,我们可以从另一个表中查看它们是否相互依赖。 If that's the case, they are treated with javascript. 如果是这种情况,可以使用javascript处理。 That's another story. 那是另一个故事。
  4. At this point, I can either create forms using custom built components in Symfony (Which would be alright). 此时,我可以使用Symfony中的自定义构建组件来创建表单(可以)。 These would output forms which worked fine, but they wouldn't be supported by sfValidator widgets, which is unfortunate. 这些将输出正常工作的表单,但是sfValidator小部件将不支持它们,这是不幸的。 What I'd like to do is somehow tell some magical form class to build a form based on X criterion, widgets and validators all. 我想做的是以某种方式告诉一些神奇的表单类,以基于X准则,小部件和验证器来构建表单。
  5. Once the form is complete and outputted, the user fills in the remaining fields, the second form gets validated and the whole whack of data from both forms is sent to the advertiser. 表单完成并输出后,用户将填写其余字段,第二个表单将通过验证,并将来自这两种表单的全部数据发送给广告商。

What I'm most interested in here is data on dynamically building forms within Symfony. 我在这里最感兴趣的是有关在Symfony中动态构建表单的数据。 I can build using a component easily, but I don't really want to. 我可以轻松地使用组件进行构建,但是我真的不想要。 Especially since I plan to quit soon, and keeping something within the framework would be ideal for my team mates. 特别是因为我计划很快退出,并且在框架内保留一些内容对于我的队友来说是理想的选择。

Thanks for any ideas! 感谢您的任何想法! This is a pretty exciting project, despite being boring from the outside - I've never dynamically generated forms before, let alone within Symfony. 尽管从外面很无聊,这是一个非常令人兴奋的项目-我以前从未动态生成过表格,更不用说在Symfony中了。 Should be cool. 应该很酷。

You can certainly build selection lists / radio buttons / check boxes based on query - im not sure if this is exactly what you want to do ? 您当然可以根据查询建立选择列表/单选按钮/复选框-我不确定这是否正是您想要的?

See http://www.symfony-project.org/api/1_4/sfWidgetFormDoctrineChoice and the query parameter - I use this a lot and it works fine. 请参阅http://www.symfony-project.org/api/1_4/sfWidgetFormDoctrineChoicequery参数-我经常使用它并且效果很好。

To build a whole form dynamically is going to be difficult IMO - I think you need to define at least some rules so that you can display / hide certain form elements. 动态创建整个表单将非常困难IMO-我认为您至少需要定义一些规则,以便可以显示/隐藏某些表单元素。

A functioning example of how I solved this problem can be found here: http://pastebin.com/NrJeADAk 有关如何解决此问题的功能示例,请参见: http : //pastebin.com/NrJeADAk

It works well. 它运作良好。 If your database describe your forms properly and consistently, you can even create new forms by submitting their descriptions via a form. 如果您的数据库正确且一致地描述了表单,则您甚至可以通过通过表单提交其描述来创建新表单。 I've done this for my team at work, and with very heavy validation on the fields, so far they haven't managed to create a single form that causes issues. 我已经为团队工作完成了此任务,并且在现场进行了大量的验证,到目前为止,他们还没有设法创建导致问题的单一表单。 I'm a frequent SO user, so if you stumble across this and would like to know more, feel free to message me. 我经常使用SO,因此如果您偶然发现此问题并想了解更多信息,请随时给我发消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM