Symfony2 Theming form generated by embedded controller action

Posted by user1112057 on Stack Overflow See other posts from Stack Overflow or by user1112057
Published on 2013-01-18T17:02:03Z Indexed on 2014/08/20 4:22 UTC
Read the original article Hit count: 238

Filed under:
|
|
|

I am generating the form in embedded controller action. And now i have faced the following problem. The Form Theming is not working in that case.

So what i have:

  1. The tempalte "page.html.twig"

    {% block content %}
    {% render 'MyBundle:Widget:index' %}
    {% endblock %}
    
  2. The indexAction() creates the form and rendering another template "form.html.twig" which is normally renders a form using form_row, form_rest and so on.

So, now i am trying to customize form theming, and here is my problem. When i put the code

{% form_theme form _self %}

in the page.html.twig, i got an error the the form variable does not exists. And its correct, the form var is created later in the embedded controller. But when i put the theming code in embedded template "form.html.twig", i got another error "Variable "compound" does not exist"

{% block form_label %}
{% spaceless %}
    {% if not compound %}
        {% set label_attr = label_attr|merge({'for': id}) %}
    {% endif %}
    {% if required %}
        {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
    {% endif %}
    {% if label is empty %}
        {% set label = name|humanize %}
    {% endif %}
    <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %} {% if attr.tooltip is defined %}title="{{ attr.tooltip }}"{% endif %}>{{ label|trans({}, translation_domain) }}{% if required %}<span>*</span>{% endif %}</label>
{% endspaceless %}
{% endblock form_label %}

This part of code was copied from this file https://github.com/symfony/symfony/blob/2.1/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

So tried someone to do something like this?

© Stack Overflow or respective owner

Related posts about forms

Related posts about symfony2