Symfony2 custom form

2019-07-20 06:24发布

I have form with checkboxes loaded from database (I use entity field type). Checkboxes are regions and districts. I have following database schema:

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

Problem is that I need following form. How to do that?

<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条回答
贪生不怕死
2楼-- · 2019-07-20 07:13

Thanks to this post I've solve this by custom rendering form template.

查看更多
成全新的幸福
3楼-- · 2019-07-20 07:27

EntityType field with options :

  • multiple = true
  • expanded = true
  • property = 'name'
  • class = 'YourBundle:YourEntity'
  • query_builder = 'function (EntityRepository $er) {return $er->createQueryBuilder('r') ->where('r.parentId IS NOT NULL') ->orderBy('r.parentId', 'ASC')->orderBy('r.name', 'ASC');}'
查看更多
登录 后发表回答