【发布时间】:2014-09-11 16:45:49
【问题描述】:
在实体 A 的形式中,我希望能够添加某种形式的实体 B。
我知道如何显示一个表单,但是当我将属性 'collection' 与 'allow_add' 放在一起时......它不会添加任何东西,只是一个空的
这是我的看法:
<.form action="{{ path('our_bundle_building_entityACreate')}}" method="post" {{ form_enctype(form) }} >
{{ form_widget(form) }}
{{ form_row(form.entity) }}
<ul id="price-fields-list" data-prototype="{{ form_widget(form.entityB.vars.prototype) | e }}">
{% for entityB in form.entityB %}
<li>
{{ form_errors(entityB) }}
{{ form_widget(entityB) }}
</li>
{% endfor %}
</ul>
<a href="#" id="add-another-entityB">Add another entityB price</a>
<input type="submit" />
<./form>
<.script type="text/javascript">
var entityBCount = '{{ form.entityB | length }}';
jQuery(document).ready(function() {
jQuery('#add-another-entityB').click(function() {
var entityBList = jQuery('#entityB-fields-list');
var newWidget = entityBList.attr('data-prototype');
newWidget = newWidget.replace(/__name__/g, entityBCount);
entityBCount++;
var newLi = jQuery('<li></li>').html(newWidget);
newLi.appendTo(jQuery('#entityB-fields-list'));
return false;
});
})
<./script>`
我的 entityB 形式运行良好,所以我不会展示它(只有几个常用的输入)
我在 buildForm 中的 entityA 形式:
->add('entityB',
'collection',
array('type' => new entityBType($this->id),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,));
顺便说一句,我并没有真正理解“原型”,所以问题可能来自这里。
感谢和问候,
我:)
【问题讨论】:
标签: forms symfony collections prototype