2015-06-14 105 views
1

我需要使用Symfony表單構建器對每個單選按鈕進行樣式設置。Symfony/Twig無線電風格formbuilder

這是我的我的createFormBuilder的一部分:

->add('categoryId', 'entity', array(
       'class' => 'MyBundle:Category', 
       'property' => 'name', 
       'required' => false, 
       'expanded' => true)) 

在我的樹枝模板:

{% for child in form.categoryId %} 
    <div class="radio i-checks col-md-3"> 
     <label>{{ form_widget(child, {'attr': { 'class': '', 'value': '' } }) }} </label> 
    </div> 
{% endfor %} 

如何顯示類的名字(現在我得到空值)?

當我使用child.get('name'),我得到這個錯誤

方法 「獲得」 爲對象 「的Symfony \分量\表格\ FormView控件」 中不存在...

回答

0

我通過編輯解決了這個問題,

位指示:

->add('categoryId', 'entity', array( 'class' => 'MyBundle:Category', 'property' => 'name', 'expanded' => true, 'multiple' => false, 'choices' => $this->getDoctrine() ->getRepository('MyBundle:Category') ->findAll(), 'required' => true, ))

在我的樹枝畫廊:

<div class="row"> {% for child in form.categoryId %} <div class="radio i-checks col-md-3"> <label>{{ form_widget(child, {'attr': { 'class': 'required', 'value': child.vars.value } }) }} {{ child.vars.label }} </label> </div> {% endfor %} </div>

我希望這會幫助別人。問候。

0

我猜

child.name 

(而無需輸入至少30個字符,我不能張貼此答案)

+0

我被試過了,但是我的對象「Symfony \ Component \ Form \ FormView」出現錯誤'Method「name」MyBundle :: add.html.twig'中不存在 – Gemmi