8

I'm rendering a form in Symfony2 with data_class mapped to Reservation entity, and this form has a entity field type, of class Service. The relation between Reservation and Service class is many to many. A service then has a ServiceType, which is another class, that is mapped as many to one from the Service class

What I want to do is display all services as checkboxes in the reservation form, grouped by service type. So far, I can display all the services together like this (the code is from ReservationType class):

$builder->add('services','entity', array(
         'class' => 'MyBundle:Service',
         'multiple' => true,
         'expanded'  => true
 ));

And displaying the form the default way:

<form action="{{ path('reservations', {'step': 2}) }}" method="post" {{    form_enctype(form) }}>
   {{ form_widget(form) }}

   <input type="submit" />
</form> 

The result is something like this:

 □ servicetype1 option
 □ servicetype1 another option
 □ servicetype2 option
 □ servicetype2 another option

What I would like to achieve is:

servicetype1:
  □ option
  □ another option
servicetype2:
  □ option
  □ another option

I was trying to specify only subsets of services by using query_builder option like this:

    $builder->add('services','entity', array(
            'class' => 'MyBundle:Service',
            'multiple' => true,
            'expanded' => true,
            'query_builder' => function (\My\Bundle\Entity\ServiceRepository $repository)
                {return $repository->createQueryBuilder('s')->where('s.serviceType = ?1')->setParameter(1, 1);} ));
    $builder->add('services','entity', array(
            'class' => 'MyBundle:Service',
            'multiple' => true,
            'expanded' => true,
            'query_builder' => function (\My\Bundle\Entity\ServiceRepository $repository)
                {return $repository->createQueryBuilder('s')->where('s.serviceType = ?1')->setParameter(1, 2);} ));

This is wrong, because:

  1. I have to specify the ServiceType id
  2. Adding the 'services' to the builder twice, will overwrite the first addition (which is logical, but cannot be solved without changing the entities)

What would be the best option for handling forms like this? There are only 2 ServiceType-s so far, but I would like to keep it dynamic, and reusable.

Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
Teo.sk
  • 2,619
  • 5
  • 25
  • 30
  • I've got [similar problem](http://stackoverflow.com/questions/9128223/symfony2-custom-form) – kuboslav Feb 07 '12 at 11:52
  • I have similar problem. This my question [link](http://stackoverflow.com/questions/9181738/symfony2-form-many-fields-for-the-same-property). When you will complete this task, you write some example? – rtyshyk Feb 08 '12 at 15:01

2 Answers2

9

I suppose the only way to do that is override rendering in the template. You should pass to your template entity MyBundle:Service and render it for example like this:

{% for service in services %}    
    <b>{{ service.name }}</b><br>
    {% for option in service.options %}                    
        <label>
            <input type="checkbox" name="form_type_name[options][{{ option.id }}]" value="{{ option.id }}" {% if option in user.services.options %}checked="checked"{% endif %}>
            {{ option.name }}
        </label>
    {% endfor %}
{% endfor %}
jkucharovic
  • 4,214
  • 1
  • 31
  • 46
  • 1
    Yes, you mean like passing it from controller to the template, avoiding using FormType for this part of the form? Doesn't this kill the re-usability a little? – Teo.sk Feb 07 '12 at 14:56
  • Ended up using this solution anyway. Thanks/Díky :) – Teo.sk Feb 08 '12 at 15:03
2

This can be solved by using the group_by option:

$builder->add('services','entity', array(
    'class' => 'MyBundle:Service',
    'group_by' => 'serviceType',
    'multiple' => true,
    'expanded'  => true
));
Jonny
  • 2,223
  • 23
  • 30
  • Great, good to see they added this option. Re-accepting this as it's much simpler. – Teo.sk May 25 '15 at 09:31
  • 4
    It's not working in this case – with option `expanded => true`. In documentation is stated: “It only works when rendered as a select tag”. – jkucharovic May 25 '15 at 10:51