0

I want to show a form choice (radio buttons) but in a special way.

The short explanation:

I need to show MANY properties from the Entity in the choice widget, not only name (_toString) and value (id).

The extense explanation:

I will not loose time explaining my Entities because they work OK and I have no problem with them.

I have a SalonWeb Entity, which has a OneToOne relationship with the Album Entity. Also the Album Entity has a OneToMany relationship with Foto Entity, and contains a $fotos ArrayCollection, and a $foto_principal property which links $foto_id.

So, with the proper doctrine query I can access something like this:

$salon_web->getAlbum()->getFotoPrincipal();

or, in TWIG:

salonWeb.album.fotoPrincipal

Until now there is all correct.

I want to show this foto (photo in english) as a form choice label, so I did this code (is working)

In the form builder:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('salones', 'entity', array(
            'class' => 'CommonBundle:SalonWeb',
            'required' => true,
            'expanded' => true,
            'query_builder' => function(EntityRepository $er)
                {
                    return $er->getQueryBuilderVisiblesContacto();
                },
            'property' => 'album.foto_principal'
        ))
    // More code...
}

...And in the TWIG template:

  <div>
      {{ form_errors(formulario.salones) }}
      {{ form_label(formulario.salones) }}
      {% for childSalon in formulario.salones %}
      <label><img src="/uploads/galeria/{{ childSalon.vars.label }}" alt="" />{{ form_widget(childSalon) }}</label>

      {% endfor %}
  </div>
  {{ form_widget(formulario) }}

Until here there is ALL WORKING fine. But the problem is that I can only show a single property in the form choice (in this case is the album.foto_principal property of the SalonWeb Entity)

I would like to show something like this:

  <div>
      {{ form_errors(formulario.salones) }}
      {{ form_label(formulario.salones) }}
      {% for childSalon in formulario.salones %}
        <label><img src="/uploads/galeria/{{ childSalon.whatever.name }}" alt="" />{{ childSalon.whatever.address ~ ' ' ~ childSalon.whatever.anotherSalonWebProperty }}
        <div>{{ childSalon.whatever.theLastProperty }}</div>
        {{ form_widget(childSalon) }}</label>

      {% endfor %}
  </div>
  {{ form_widget(formulario) }}
Dani Sancas
  • 1,365
  • 11
  • 27
  • I found a valid answer [here](http://stackoverflow.com/questions/13070595/symfony-2-create-a-entity-form-field-with-2-properties) But maybe I had to write some HTML code inside that method (and maybe isn't the best option as we use MVC pattern) – Dani Sancas Jul 03 '13 at 08:26

1 Answers1

2

Finally I found a way to do that, inspired by the solution of this post:

Symfony 2 Create a entity form field with 2 properties

Add a method to my SalonWeb Entity:

    //...
    public function getFormChoiceImageAndLabelProperties()
    {
        return array(
            'image_src' => $this->getAlbum()->getFotoPrincipal(),
            'label' => $this->getDireccionParcial(),
            'another_property' => $this->getWhatever(),
        );
    }

Change my Form builder choice property from

'property' => 'album.foto_principal'

to

'property' => 'form_choice_image_and_label_properties'

...And in the TWIG template:

  <div>
      {{ form_errors(formulario.salones) }}
      {{ form_label(formulario.salones) }}
      {% for childSalon in formulario.salones %}
      <label>
        <img src="/uploads/galeria/{{ childSalon.vars.label['image_src'] }}" alt="{{ childSalon.vars.label['label'] }}" />
        <div>
          <span>{{ childSalon.vars.label['label'] }}</span>
        </div>
        <div>
          <span>{{ childSalon.vars.label['another_property'] }}</span>
        </div>
        {{ form_widget(childSalon) }}
      </label>

      {% endfor %}
  </div>
  {{ form_widget(formulario) }}
  {{ form_widget(formulario) }}
Community
  • 1
  • 1
Dani Sancas
  • 1,365
  • 11
  • 27