简体   繁体   English

Symfony2表单主题针对各个领域

[英]Symfony2 form theming for individual field

I am building forms using Symfony2 form builder and using bootstrap_3_horizontal_layout.html.twig theme (it is the default for all my forms), but I need to customize the layout for a specific field content_owner . 我正在使用Symfony2表单构建器和bootstrap_3_horizontal_layout.html.twig主题(这是我所有表单的默认表单)构建表单,但是我需要自定义特定字段content_owner的布局。 It is a multiple select , so I am using the code below to override the default template: 这是一个multiple select ,因此我正在使用以下代码覆盖默认模板:

{% block choice_widget_collapsed %}
<div class="col-md-4"><div class="form-group">{{- block('form_widget_compound') -}}</div> </div>
{% endblock %}

and in new.html.twig (where the form renders): 并在new.html.twig中(表单呈现的位置):

{% for name, child in form.content if name != 'graphics' %}
                        {{ form_row(child) }}
                    {% endfor %}
{{ form_widget(form.content.content_owner) }}

The output I am getting (in firebug): 我得到的输出(在萤火虫中):

<div class="form-group">
<label for="form_content_content_owner" class="col-sm-2 control-label">Content owner</label>
   <div class="col-sm-10">
    <div class="col-md-4"> 
       <div class="form-group">
            <div id="form_content_content_owner">

             <!--and it is blank. no choice widget--> 
             </div>
                </div>
                   </div>
                      </div>
                          </div>

The code breaks all of my choice widgets ,that is,no select is visible. 该代码破坏了我所有的choice widgets ,即没有select可见。 What am I doing wrong? 我究竟做错了什么?

Desired output: 所需的输出:

  <div class="col-sm-4">
   <div class="form-group">
      <label for="form_content_content_owner" class="col-sm-2 control-label">Content owner</label>
         <div class="col-sm-10">
           <select class="form-control" name="form[content][content_owner]" id="form_content_content_owner">
             <option value=""></option>
             <option value="1">Anupam</option>
               <!--option value goes on-->
             <option value="31">World Com</option>
           </select>
       </div>
    </div>
  </div>

I think you will find that the choice_widget_collapsed block is rendered inside a form_widget_compound block, so you've set up an infinite loop. 我想你会发现choice_widget_collapsed块是一个内部渲染form_widget_compound块,所以你已经设置了一个无限循环。

Try adding a debugger breakpoint into Symfony\\Component\\Form\\FormRenderer.php at around line 250 just after where this code is: 尝试将调试器断点添加到Symfony\\Component\\Form\\FormRenderer.php中的第250行附近,此代码位于以下位置:

$blockName = $blockNameHierarchy[$hierarchyLevel];

The look at what value $blockName takes during each iteration. 查看$blockName在每次迭代期间取什么值。 Once you get to the field you are trying to render you should see which blocks are being called and in which order. 到达要渲染的字段后,您应该看到正在调用的块以及调用的顺序。 This might help you understand what's happening in your templates a little better. 这可以帮助您更好地了解模板中正在发生的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM