Symfony2 自定义表单

发布于 2025-01-01 22:42:20 字数 1152 浏览 1 评论 0原文

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

+-----------------------------------+
| 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜雨飘雪 2025-01-08 22:42:20

感谢这篇文章,我已经通过自定义呈现表单模板解决了这个问题。

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

蓝海 2025-01-08 22:42:20

带有选项的 EntityType 字段:

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

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');}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文