Symfony2自定义表单,替换属性,值和主题

时间:2014-05-01 04:05:25

标签: php html symfony

我是Symfony的新手。我已经阅读了Symfony的手册并搜索了网页,但仍无法找到此问题的示例。

这是“new.html.twig”

{% block content %}

{% form_theme form _self %}
{% block _sifo_schoolbundle_mstperiode_nama_widget %}
<div class="form-group">    
    <div class="col-sm-10">
        {{ form_errors(form) }}
        {{ form_widget(form) }}
    </div>
</div>
{% endblock %}
 {{ form_widget(form) }}
                   </div>
              </section>
{% endblock %}

结果如下:

<div class="form-group">    
    <div class="col-sm-10">     
        <div id="sifo_schoolbundle_mstperiode">
            <div>
                <label for="sifo_schoolbundle_mstperiode_nama" class="required">Nama</label>
                <div class="form-group">
                    <div class="col-sm-10">
                        <input type="text" id="sifo_schoolbundle_mstperiode_nama" name="sifo_schoolbundle_mstperiode[nama]" required="required" maxlength="50" />
                    </div>
                </div>
            </div>
            <div>
                <label for="sifo_schoolbundle_mstperiode_keterangan">Keterangan</label>
                <input type="text" id="sifo_schoolbundle_mstperiode_keterangan" name="sifo_schoolbundle_mstperiode[keterangan]" maxlength="250" />
            </div>
            <div>
                <label for="sifo_schoolbundle_mstperiode_aktif">Aktif</label>
                <input type="checkbox" id="sifo_schoolbundle_mstperiode_aktif" name="sifo_schoolbundle_mstperiode[aktif]" value="1" />
            </div>
    </div>
</div>

我想这样做:

<div class="form-group">
    <label for="sifo_schoolbundle_mstperiode_nama" class="col-sm-2 col-sm-2 control-label">Default Here</label>
    <div class="col-sm-10">
       <input type="text" id="sifo_schoolbundle_mstperiode_nama" name="sifo_schoolbundle_mstperiode[nama]" required="required" maxlength="50" class="form-control" />
    </div>
</div>
<div>
    <label for="sifo_schoolbundle_mstperiode_keterangan">Keterangan</label>
    <input type="text" id="sifo_schoolbundle_mstperiode_keterangan" name="sifo_schoolbundle_mstperiode[keterangan]" maxlength="250" />
</div>
div>
    <label for="sifo_schoolbundle_mstperiode_aktif">Aktif</label>
    <input type="checkbox" id="sifo_schoolbundle_mstperiode_aktif" name="sifo_schoolbundle_mstperiode[aktif]" value="1" />
</div>

结论:

  1. 如何在<label for="sifo_schoolbundle_mstperiode_nama" class="col-sm-2 col-sm-2 control-label">Default Here</label>内但<div class="form-group"></div>
  2. 之外制作标签<div class="col-sm-10"><div>
  3. 如何将<label for="sifo_schoolbundle_mstperiode_nama" class="required">Nama</label>中的类和值替换为此<label for="sifo_schoolbundle_mstperiode_nama" class="col-sm-2 col-sm-2 control-label">Default Here</label>
  4. 如何在<input type="text" id="sifo_schoolbundle_mstperiode_nama" name="sifo_schoolbundle_mstperiode[nama]" required="required" maxlength="50" />
  5. 中添加课程

1 个答案:

答案 0 :(得分:2)

Symfony使用以下块进行渲染 https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

所以你应该定义

{% block form_row %}
<div class="form-group">
  {% form_label(form) %}
  <div class="col-sm-10">
    {% form_widget(form) %}
    {% form_errors(form) %}
  </div>
</div>
{% endblock %}

等等,以满足您的HTML。

或者尝试使用任何现有的symfony bootstrap包。 例如。这一个https://github.com/braincrafted/bootstrap-bundle