且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Symfony2自定义表单

更新时间:2023-11-19 19:07:52

感谢这个帖子我已经通过自定义渲染表单模板解决了这个问题。


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>

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