【问题标题】:symfony 2, form_rest(form) shows collection fieldssymfony 2,form_rest(form) 显示集合字段
【发布时间】:2013-07-18 08:47:08
【问题描述】:

我的表单有 3 个集合字段:

$builder->add('affiliates', 'collection', array(
    'type' => new AffiliateForm(),
    'allow_add' => true, 
    'allow_delete' => true, 
    'by_reference' => false,
    'options' => array(
        'affiliate_types' => $options['affiliate_types'],
        'business_types' => $options['business_types'],
    ),
));

$builder->add('other_businesses', 'collection', array(
    'type' => new OtherBusinessForm(),
    'allow_add' => true, 
    'allow_delete' => true, 
    'by_reference' => false,
));

$builder->add('welfare_activities', 'collection', array(
    'type' => new WelfareActivityForm(),
    'allow_add' => true, 
    'allow_delete' => true, 
    'by_reference' => false,
    'options' => array(
        'welfare_activity_types' => $options['welfare_activity_types'],
    ),
));

在模板中,我将每个子表单字段一一单独显示,如下所示:

<td class="t1c5" >{{ form_widget(affiliate.location) }}
                  {{ form_errors(affiliate.location) }}</td>

在表格的最后:

{{ form_rest(form) }}

但是当给定集合为空时,它会导致在表格末尾显示以下单词:“附属公司”、“其他企业”、“福利活动”。 所以问题是:

  1. 为什么这些词会显示在表单上?
  2. 我可以采取以下措施来避免上述问题:

    <div style="display:none;">{{ form_rest(form) }}</div>    
    

    这是处理问题的正确方法(也许我可以隐藏一个字段或其他什么)?

谢谢。

【问题讨论】:

    标签: forms symfony collections


    【解决方案1】:

    这些词显示在表格上是因为您忘记了:

    {{ form_label(affiliate.location) }}
    …
    …  
    

    根据文档:

    form_rest(视图、变量)

    这会呈现给定表单尚未呈现的所有字段。最好始终将它放在表单中的某个位置,因为它会为您呈现隐藏字段并使您忘记呈现的任何字段更加明显(因为它会为您呈现字段)。

    【讨论】:

      【解决方案2】:
          {{ form_end(form, {'render_rest': false}) }}
      

      【讨论】:

        猜你喜欢
        • 2011-12-01
        • 1970-01-01
        • 2012-06-09
        • 2012-04-11
        • 2012-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多