简体   繁体   English

Symfony2:如何将参数传递给表单集合

[英]Symfony2: How to pass parameters to form collections

I'm using a form(parent form) with a collection field type(child forms). 我正在使用带有集合字段类型(子表单)的表单(父表单)。 Each type in the collection is a form that contains some choice fields, I want to filter this field choices using query_builder parameter for join with other entities ect. 集合中的每个类型都是包含一些选项字段的表单,我想使用query_builder参数过滤此字段选项以与其他实体ect连接。 I know how to pass parameters to parent form from controller, but how to pass parameters to each item in the collection (child forms)? 我知道如何从控制器传递参数到父窗体,但如何将参数传递给集合中的每个项目(子窗体)?

What you want is a data transformer. 你想要的是数据转换器。 The data transformer takes data from your sub forms allows you to do queries to the database from inside it and pass 1 final result out the end. 数据转换器从子表单中获取数据,允许您从内部对数据库进行查询,并将最终结果传递给最终。 I had to do this the other day with country and postal code to find a valid postal code in a country. 前几天我不得不用国家和邮政编码在一个国家找到有效的邮政编码。

Here's the link to the cookbook info that will show you how to use it. 这是菜谱信息的链接,它将向您展示如何使用它。

http://symfony.com/doc/current/cookbook/form/data_transformers.html http://symfony.com/doc/current/cookbook/form/data_transformers.html

Here is my answer: https://stackoverflow.com/a/17223519/1756517 这是我的答案: https//stackoverflow.com/a/17223519/1756517

In each form, pass the variable by the construct method ($form = new FormType($your_variable);) 在每种形式中,通过构造方法传递变量($ form = new FormType($ your_variable);)

A bit late response, but with Symfony3, you should use the entry_options parameter as mentioned here . 有点迟到的响应,但是使用Symfony3,你应该使用这里提到entry_options参数。 It was exactly what I needed, in scenario similar to yours. 在你和你的情况类似的情况下,这正是我所需要的。

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

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