Aximem
Aximem

Reputation: 732

Symfony | Twig get field datas when display form

I have a simple form to filter a list in function of themes :

$builder
        ->add('themes', EntityType::class, array(
            'class'    => 'XXBundle:Theme',
            'choice_label' => 'image',
            ...
        ))
        ->add('save', SubmitType::class)
    ;

And in Twig I try to access all themes to do a specific render, in fact I would like to display Image (related to each theme : theme.image) instead of the theme.

I followed other solutions : Symfony2 + CreateFormBuilder how to render an image in a form

But it doesn't work :

form.vars.value.themes / form.vars.data.themes (or form.themes.vars.value / form.themes.vars.data this is the same) 

exists but are always empty. Because this is a new form. If I submit the form, it works form.vars are not empty anymore.

How can I get themes when I display the form for the first time ? I followed the doc (http://symfony.com/doc/current/reference/forms/twig_reference.html#form-variables-reference) but I can't find what I want.

TY

Upvotes: 1

Views: 692

Answers (1)

Matteo
Matteo

Reputation: 39380

Basically the EntityType is a List of choices - form.themes.vars.choices. Its an array of ChoiceView, to get entity simply access public data property.

So use this property to access to the entities:

form.themes.vars.choices

More accessible attribute are listed in the doc here.

Hope this help

Upvotes: 1

Related Questions