Symfony2 custom form

柔情痞子 提交于 2019-12-11 01:36:12

问题


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>

回答1:


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




回答2:


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');}'


来源:https://stackoverflow.com/questions/9128223/symfony2-custom-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!