2013-10-23 29 views
1

要嫩枝整合這樣的:是否可以用一個form_label在Twig中包裝form_widget?

<label class="control-label" for="lastname">Last Name:</label> 
<div class="controls"> 
    <input type="text" id="firstname" name="firstname"> 
    <span class="help-block">{{ form_errors(form.firstname) }}</span> 
</div> 

我用下面的代碼片段:

{{ form_label(form.firstname, null, {'label_attr': {'class': 'control-label'}}) }} 
<div class="controls"> 
    {{ form_widget(form.firstname) }} 
    <span class="help-block">{{ form_errors(form.firstname) }}</span> 
</div> 

,一切工作正常。

但我的問題是...

是否可以換一個form_widget在根樹枝form_label?那麼最終的結果應該是類似到:

<label class="radio" for="dn"> 
    <input type="radio" id="dn" name="spotlight" value="1"> Test 1 
</label> 
<label class="radio" for="gn"> 
    <input type="radio" id="gn" name="spotlight" value="1"> Test 2 
</label> 
<span class="help-block">Errors</span> 

我可以使用別的比form_labelform_widget才達到同樣的結果?

回答

1

可以更改form_row()輸出的顯示,只有一個文件:

{% form_theme form _self %} 

{% block form_row %} 
    <div class="form_row"> 
     {% if label is not sameas(false) %} 
      {% 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 %}>{{ label|trans({}, translation_domain) }} 
     {% endif %} 

     {{ form_widget(form) }} 

     {% if label is not sameas(false) %} 
      </label> 
     {% endif %} 

     {{ form_errors(form) }} 
    </div> 
{% endblock form_row %} 

,並顯示你的領域:

{{ form_row(form.firstname) }} 

<label>現在打開前場和後關閉場。

我從default theme取了原始碼並使用cookbook entry Form Theming in Twig > Method 1: Inside the same Template as the Form

如果您想將您的想法應用於所有字段,請考慮使用form customization以更改所有包中的{{ form_row(....) }}的顯示。

相關問題