Symfony2 native form

I have a form with checkboxes loaded from the database (I use the field type of the object). Flags are regions and areas. I have the 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 | +-----------------------------------+ 

The problem is that I need the following form. How to do it?

 <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
forms symfony doctrine2 symfony-forms
source share
2 answers

Thanks to this post, I solved this by creating a personalized form template.

+1
source share

EntityType field with parameters:

  • multiple = true
  • extended = 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');} '
0
source share

All Articles