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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝