2015-07-10 123 views
5

我有一個包含多個購物車的模板。可以有不定數量的推車,沒有固定的限制。如何在Symfony2中多次呈現相同的表單?

在每個購物車中,我想要一個表單,用戶可以選擇一個國家。如果他提交表格,應該確定運輸成本。

現在我做以下,以實現它在樹枝:

{% for cart in carts %} 
    {# Some template stuff #} 
    {{ form_start(form) }} 
     <div class="form-input"> 
     <label for="country" class="middle-color">Country <span class="active-color">*</span></label> 
     {{ form_widget(form.country) }} 
    {{ form_end(form) }} 
{% endfor %} 

這是我的表單生成器:

$form = $this->createFormBuilder() 
    ->add('country', 'choice', array('choice_list' => $choiceList, 'label' => 'country', 
     'attr' => array('class' => "custom-selectbox dark-color light-gradient"))) 
    ->getForm(); 

現在的問題是,這種邏輯正常工作的第一臺車,但沒有任何形式顯示給其他購物車。我該如何處理這個問題?

回答

-1

您應該使用collection表單類型。這裏是一個指南開始How to Embed a Collection of Forms

P.S.請注意,渲染表單窗口小部件後,表單組件將其標記爲呈現狀態,並且不再呈現。

+0

我看不出這會幫助我。我創建一個choice_list取決於外部數據。我不想多次顯示choice_list,而是整個表單 – KhorneHoly

2

我遇到了這個問題和關於類似問題的另一個問題。 You can find my first answer for a solution here

爲了把它包起來,我沒有在控制器窗體上調用createView()函數,就像通常在將窗體傳遞給視圖時一樣,但是在樹枝視圖本身中。

E.g.在你的控制器動作你做的形式返回對象本身:

return $this->render('AppBundle:Cart:list.html.twig', ['formObject' => $form]; 

,並在你看來,你會設置的形式,每個循環:

{% for cart in carts %} 
    {# Some template stuff #} 
    {% set form = formObject.createView %} 
    {{ form_start(form) }} 
     <div class="form-input"> 
     <label for="country" class="middle-color">Country <span class="active-color">*</span></label> 
     {{ form_widget(form.country) }} 
    {{ form_end(form) }} 
{% endfor %} 
相關問題