Forms Symfony 2:如何在表单的集合中正确添加元素
在entityA的形式中,我希望能够添加某种形式的entityB 我知道如何显示一个表单,但当我将属性“collection”与“allow_add”放在一起时。。。除了一个空的 以下是我的看法:Forms Symfony 2:如何在表单的集合中正确添加元素,forms,symfony,collections,prototype,Forms,Symfony,Collections,Prototype,在entityA的形式中,我希望能够添加某种形式的entityB 我知道如何显示一个表单,但当我将属性“collection”与“allow_add”放在一起时。。。除了一个空的 以下是我的看法: <.form action="{{ path('our_bundle_building_entityACreate')}}" method="post" {{ form_enctype(form) }} > {{ form_widget(form) }} {{ form_row(form
<.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>`
顺便说一下,我没有真正理解“原型”,所以问题可能来自这里
感谢和问候
Me:)如果您只想嵌入一个表单,最简单的方法是:
...
->add('entityB', new entityBType($this->id));
我需要一个表单的集合,正如我所说的,我已经知道这是可行的,但是集合不是很重要吗?你真的需要构建一个entityB表单的集合吗?因为你上面的例子不清楚。如果它只是一个要嵌入的表单,那么从功能上来说,“集合”是不必要的。一个子表单的集合不是集合。:/在这种情况下,只需将entityB表单类型设置为父entityA表单类型的子属性。否则,请查看
app/logs
中的日志文件,看看Symfony是否在抱怨您的收藏配置。上面的代码片段不足以确定问题出在哪里。事实上entityA与entityB有一对多的关系,所以我想我需要一个集合。我将检查应用程序/日志
...
->add('entityB', new entityBType($this->id));