【发布时间】:2014-07-19 06:47:35
【问题描述】:
我正在尝试创建一个表单集合,其中包含 3 个全局部分和第四部分,其中包含允许复制的 3 个字段。两个表单名称都是“搜索”。我不知道我做错了什么,但在原型中而不是创建整个表单,我得到以下内容:
<input type="search" id="search_statistics_collection___name__" name="search[statistics_collection][__name__]" required="required" class="form-control" ><a href='#' class='add-statistic btn'>Dodaj statystykę</a>
查看:
{{ form_start(searchForm) }}
{{ form_row(searchForm.league) }}
{{ form_row(searchForm.range) }}
{{ form_row(searchForm.season) }}
<div id="single-proto" data-prototype="{{ form_widget(searchForm.statistics_collection.vars.prototype)|e }}<a href='#' class='add-statistic btn'>Dodaj statystykę</a>"></div>
{% for single in searchForm.statistics_collection %}
<div class="single-statistic">
{{ form_label(single.statistic) }}
{{ form_widget(single.statistic) }}
{{ form_widget(single.sign) }}
{{ form_widget(single.value) }}
<a href="#" class="add-prototype btn">Dodaj statystykę</a>
</div>
{% endfor %}
{{ form_end(searchForm) }}
表单类:(相关部分)
搜索类型.php:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('league', 'choice', array(
'label'=>'Wybierz ligi',
'choices'=>array(
'all'=>'Wszystkie ligi',
'favourite'=>'Moje ulubione ligi',
'bookie'=>'Ligi mojego bukmachera'
)
))
->add('range', 'choice', array(
'label'=>'Wybierz zakres',
'choices'=>array(
'all'=>'Cały mecz - wszystkie mecze',
'home'=>'Cały mecz - dom',
'guest'=>'Cały mecz - wyjazd',
'half_all'=>'Do przerwy - wszystkie mecze',
'half_home'=>'Do przerwy - dom',
'half_guest'=>'Do przerwy - wyjazd',
)
))
->add('season', 'choice', array(
'label'=>'Wybierz sezon',
'choices'=>Statistics::$seasons
))
->add('statistics_collection', 'collection', array(
'label'=>'Wybierz statystykę',
'type' => new SearchSubType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
))
->setData(array(
'statistics_collection' => array(
array('', '', ''),
)
))
;
}
SearchSubType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('statistic', 'choice', array(
'label'=>'Wybierz statystykę',
'required'=>false,
'choices'=>Statistics::$statistics
))
->add('sign', 'choice', array(
'label'=>'',
'required'=>false,
'choices'=>array(
'gt'=>'>',
'lt'=>'<',
'eq'=>'=',
'gteq'=>'>=',
'lteq'=>'<='
)
))
->add('value', 'text', array(
'label'=>'',
'required'=>false,
))
;
}
请帮忙:)
解决方案
集合中包含的子表单必须具有不同于父表单的名称。否则一切都搞砸了:)
【问题讨论】: