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) }}