Symfony2自定义表单

时间:2012-02-03 11:50:53

标签: forms symfony doctrine-orm symfony-forms

我有从数据库加载的复选框的表单(我使用实体字段类型)。复选框是区域和区域。我有以下数据库架构:

+-----------------------------------+
| id | parent_id | name             |
+-----------------------------------+
| 1  | NULL      | Region           |
+-----------------------------------+
| 2  | 1         | District         |
+-----------------------------------+
| 3  | 1         | Another district |
+-----------------------------------+
| 4  | NULL      | Another region   |
+-----------------------------------+
| 5  | 4         | Next district    |
+-----------------------------------+

问题是我需要以下表格。怎么做?

<b>Region</b><!-- Loaded from database -->
<!-- Dictricts ordered by name -->
<input type="checkbox" id="someId" value="3"><label for="someId">Another district</label>
<input type="checkbox" id="someId" value="2"><label for="someId">District</label>
<b>Another region</b><!-- Loaded from database -->
<!-- Dictricts ordered by name -->
<input type="checkbox" id="someId" value="5"><label for="someId">Next district</label>

2 个答案:

答案 0 :(得分:1)

感谢this post我通过自定义呈现表单模板解决了这个问题。

答案 1 :(得分:0)

带有选项的

EntityType字段:

  • multiple = true
  • expanded = true
  • property ='name'
  • class ='YourBundle:YourEntity'
  • query_builder ='function(EntityRepository $ er){return $ er-&gt; createQueryBuilder('r')                     - &gt; where('r.parentId IS NOT NULL')                     - &gt; orderBy('r.parentId','ASC') - &gt; orderBy('r.name','ASC');}'