doupin1073 2017-06-12 08:28
浏览 56
已采纳

Symfony3表单构建器表单字段,而不是div

I have Symfony3 app and I am making a simple form the code in the twig is as follows

 {{ form_start(edit_form) }}
      {{ form_widget(edit_form) }}
      <input type="submit" value="Edit" />
 {{ form_end(edit_form) }}

Pretty simple. What this code creates is a form and each form field is within it's own <div> which is fine, but if the type is date here is what the generated html looks like

<div>
  <label class="required">Term</label>
    <div id="appbundle_project_term">
      <select id="appbundle_project_term_year" name="appbundle_project[term][year]"></select>
      <select id="appbundle_project_term_year" name="appbundle_project[term][month]"></select>
      <select id="appbundle_project_term_year" name="appbundle_project[term][day]"></select>
    </div>
</div>

What bugs me is the inner div created for the date type field. Is there a way in the FormBuilder to keep the type date but remove this inner div without using javascript to handle it or in the twig template. Simply to say - "inner tag => span". This is pretty generic question as I am looking for a way to usually change the auto generated tags, but if needed here is how this form field is created in form builder

add('term',DateType::class, array(
            'widget' => 'choice',
            'label'=>"Term",
            'data'=>$project->getTerm()
        ))
  • 写回答

1条回答 默认 最新

  • douchen7366 2017-06-12 11:05
    关注

    You can override form rendering, there are few ways.

    The simplest one is overriding form theme widget block (in this case date_widget) and setting form_theme to _self.

    Basic example:

    {% form_theme form _self %}
    
    {% block date_widget %}
        <span>
            {% if widget == 'single_text' %}
                {{ block('form_widget_simple') }}
            {% else %}
                {# rendering 3 fields for year, month and day #}
                {{ form_widget(form.year) }}
                {{ form_widget(form.month) }}
                {{ form_widget(form.day) }}
            {% endif %}
        </span>
    {% endblock %}
    
    {% block content %}
        {# ... form rendering #}
    
        {{ form_row(form.someDateField) }}
    {% endblock %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要