How to get an entity or pass a variable to a form widget of a Symfony2 widget?

In my edit.html.twig, I have:

{% form_theme edit_form 'MyBundle:Entity:form.html.twig' %} {% set img_src = asset('120x100.jpg') %} {{ dump(img_src) }} {{ dump(entity) }} {{ form_widget(edit_form, {'form_type': 'horizontal', 'img_src': img_src }) }} 

I have img_src and an entity , no problem.
In form.html.twig, I have:

 {% extends 'MyBundle:Form:bootstrap.html.twig' %} {% block _entity_field_widget %} {{ dump(img_src) }} {{ dump(entity) }} {% set type = 'hidden' %} {{ block('form_widget_simple') }} {% endblock _channel_media_widget %} 

bootstrap.html.twig is just a boot file * form_div_layout.html.twig *
And in this widget I don't have img_src or entity .
Any ideas on how to get an object in widgets? Should it be passed to form a widget or is there another way? What am I doing wrong?

+7
source share
1 answer

Each symfony form type extends the AbstractType class.

The AbstactType class has a method:

 public function buildView(FormView $view, FormInterface $form, array $options) { $view->set('img_src', '120x100.jpg'); $view->set('my_variable', $foo); } 

You can create this method in your form type and the following in your branch:

 {{ asset(img_src) }} 
+8
source

All Articles