2015-06-10 27 views
1

我有一個動態表單來創建,並且我想要擴展一個小部件以將其顯示爲<tr>。 事實是,表中的每一列都是包含一些字段的動態組。將變量插入到擴展樹枝中form_theme

所有我想要做的就是在myform_widget這樣的疊代羣體:

下面是表頭

<table id="myforms-table" class="table"> 
    <thead> 
    <tr> 
     {% for group in groups %}<th>{{ group.label }}</th>{% endfor %} 
    </tr> 
    </thead> 
    <tbody data-prototype="{{ form_widget(form.myforms.vars.prototype)|e }}"> 

    </tbody> 
</table> 

這裏是myForm的塊:

{% block myform_widget %} 
    <tr> 

     {% for group in groups %} 
      <td> 
       {% for field in group.fields %} 
        {{ form_row(form.children.(field.codeField)) }} 
       {% endfor %} 
      </td> 
     {% endfor %} 

    </tr> 
{% endblock %} 

我獲得例外Variable "groups" does not exist。 我認爲form_theme無法訪問groups變量,但我如何才能訪問它? strong text 謝謝o/

回答

1

我找到了一個簡單的解決方案。

在我的表單類型的文件我已經添加了groups ATTR,這樣的:

public function buildView(FormView $view, FormInterface $form, array $options) 
{ 
    $view->vars['groups'] = $this->groups; 
} 

現在我能與檢索:

{% block partition_widget %} 
    <tr> 
     {% for group in form.vars.groups %} 
      <td> 
       {% for field in group.fields %} 
        {{ form_row( attribute(form, field.codeField)) }} 
       {% endfor %} 
      </td> 
     {% endfor %} 
    </tr> 
{% endblock %}