2017-06-12 08:28
浏览 56


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

  <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>

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',

图片转代码服务由CSDN问答提供 功能建议

我有一个Symfony3应用程序,我正在制作一个简单的表格,树枝中的代码如下 \ n

&lt; input type =“submit”value =“Edit”/&gt; 
 {{form_end  (edit_form)}} 

非常简单。 这段代码创建的是一个表单,每个表单字段都在它自己的&lt; div&gt; 中,这很好,但如果类型是日期,那么生成的html就是这样的

 &lt; div&gt; 
&lt; label class =“required”&gt;术语&lt; / label&gt; 
&lt; div id =“appbundle_project_term”&gt; 
&lt; select id =“appbundle_project_term_year  “name =”appbundle_project [term] [year]“&gt;&lt; / select&gt; 
&lt; select id =”appbundle_project_term_year“name =”appbundle_project [term] [month]“&gt;&lt; / select&gt; 
&lt;  ; select id =“appbundle_project_term_year”name =“appbundle_project [term] [day]”&gt;&lt; / select&gt; 
&lt; / div&gt; 
&lt; / div&gt; 

我的错误是为日期类型字段创建的内部 div 。 FormBuilder中是否有一种方法可以保留类型日期,但删除此内部div而不使用javascript来处理它或在树枝模板中。 简单地说 - “inner tag =&gt; span”。 这是非常通用的问题,因为我正在寻找一种通常更改自动生成的标签的方法,但如果需要,这里是如何在表单构建器中创建此表单字段

  add('term',DateType :: class,array(
'label'=&gt;“Term”,\  n'data'=&gt; $ project-&gt; 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 %}
            {% 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( }}
            {% endif %}
    {% endblock %}
    {% block content %}
        {# ... form rendering #}
        {{ form_row(form.someDateField) }}
    {% endblock %}
    打赏 评论

相关推荐 更多相似问题