Symfony 具有相同实体类型集合的窗体
我想要我的实体«X»的表格。该实体与许多«X»类型的实体有一个关系。这是一种亲子关系 当我简单地创建表单时,它就工作了Symfony 具有相同实体类型集合的窗体,symfony,Symfony,我想要我的实体«X»的表格。该实体与许多«X»类型的实体有一个关系。这是一种亲子关系 当我简单地创建表单时,它就工作了 class XType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add("name", "text", array("l
class XType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("name", "text", array("label" => "Nom"))
->add("children", "collection", array(
"type" => new XType(),
"by_reference" => false));
}
}
然后,我想在我的集合中使用选项“allow_add”轻松添加新实体,并使用原型添加javascript。这是我的表格,带有“允许添加”选项
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("name", "text", array("label" => "Nom"))
->add("children", "collection", array(
"type" => new XType(),
"allow_add" => true,
"by_reference" => false));
}
当我在调用或不调用原型的情况下执行时,我有一个Web服务器错误。是XDebug终止了我的请求,因为递归调用太大了。有级联调用
解决我的问题的最佳解决方案是什么?我真的不能说你的问题在哪里,但应该在javascript和集合原型周围。此外,当您允许添加项目时,通常也允许删除 看看我的例子:
$builder->add('book', 'collection', array(
'type' => new BookType(),
'allow_add' => true,
'allow_delete' => true,
'prototype_name' => '__prototype__',
'by_reference' => false,
'error_bubbling' => false
);
对于渲染表单的模板,请包括以下内容:
{% block javascript %}
{{ parent() }}
{% javascripts
'@ProjectSomeBundle/Resources/public/js/form/collection.js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
此外,您应该使用这些定制的集合小部件,并将其传递给twig。没什么特别的,它将被呈现为带有项目的无序列表,以使项目易于访问
config.yml:
twig:
form:
resources:
- 'ProjectSomeBundle:Form:fields.html.twig'
fields.html.twig:
{% extends 'form_div_layout.html.twig' %}
{% block collection_widget %}
{% import "ProjectSomeBundle:Macro:macro.html.twig" as macro %}
{% spaceless %}
<div class="collection">
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': block('prototype_widget') }) %}
{% endif %}
<div {{ block('widget_container_attributes') }}>
<ul>
{% for row in form %}
{{ macro.collection_item_widget(row) }}
{% endfor %}
</ul>
{{ form_rest(form) }}
</div>
<div class="clear"></div>
<button type="button" class="add">{% trans %}Add{% endtrans %}</button>
</div>
<div class="clear"></div>
{% endspaceless %}
{% endblock collection_widget %}
{% block prototype_widget %}
{% spaceless %}
{{ macro.collection_item_widget(prototype) }}
{% endspaceless %}
{% endblock prototype_widget %}
{%extends'form\u div\u layout.html.twig%}
{%block collection_widget%}
{%import“ProjectSomeBundle:Macro:Macro.html.twig”作为宏%}
{%spaceless%}
{%如果定义了原型%}
{%set attr=attr | merge({'data-prototype':块('prototype_小部件')})%}
{%endif%}
{表格%中的行的%1}
{{macro.collection\u item\u小部件(行)}
{%endfor%}
{{form_rest(form)}
{%trans%}添加{%endtrans%}
{%endspaceless%}
{%endblock集合\小部件%}
{%block prototype_widget%}
{%spaceless%}
{{macro.collection\u item\u小部件(原型)}
{%endspaceless%}
{%endblock原型_widget%}
您可以注意到它使用宏,因此它是:
macro.html.twig
{% macro collection_item_widget(fields) %}
<li>
{% set fieldNum = 1 %}
{% for field in fields %}
<div class="field_{{ fieldNum }}">
{{ form_label(field) }}
{{ form_errors(field) }}
{{ form_widget(field) }}
</div>
{% set fieldNum = fieldNum + 1 %}
{% endfor %}
<button type="button" class="remove">{% trans %}Delete{% endtrans %}</button>
<div class="clear"></div>
</li>
{% endmacro %}
{%macro-collection\u-item\u小部件(字段)%}
{%set fieldNum=1%}
{字段%中的字段为%1}
{{form_标签(字段)}}
{{form_errors(field)}}
{{form_widget(field)}
{%set fieldNum=fieldNum+1%}
{%endfor%}
{%trans%}删除{%endtrans%}
{%endmacro%}
这是一个完整的示例,我希望您觉得它很有用,对您有用。我真的不能说您的问题在哪里,但应该在javascript和集合原型周围。此外,当您允许添加项目时,通常也允许删除 看看我的例子:
$builder->add('book', 'collection', array(
'type' => new BookType(),
'allow_add' => true,
'allow_delete' => true,
'prototype_name' => '__prototype__',
'by_reference' => false,
'error_bubbling' => false
);
对于渲染表单的模板,请包括以下内容:
{% block javascript %}
{{ parent() }}
{% javascripts
'@ProjectSomeBundle/Resources/public/js/form/collection.js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
此外,您应该使用这些定制的集合小部件,并将其传递给twig。没什么特别的,它将被呈现为带有项目的无序列表,以使项目易于访问
config.yml:
twig:
form:
resources:
- 'ProjectSomeBundle:Form:fields.html.twig'
fields.html.twig:
{% extends 'form_div_layout.html.twig' %}
{% block collection_widget %}
{% import "ProjectSomeBundle:Macro:macro.html.twig" as macro %}
{% spaceless %}
<div class="collection">
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': block('prototype_widget') }) %}
{% endif %}
<div {{ block('widget_container_attributes') }}>
<ul>
{% for row in form %}
{{ macro.collection_item_widget(row) }}
{% endfor %}
</ul>
{{ form_rest(form) }}
</div>
<div class="clear"></div>
<button type="button" class="add">{% trans %}Add{% endtrans %}</button>
</div>
<div class="clear"></div>
{% endspaceless %}
{% endblock collection_widget %}
{% block prototype_widget %}
{% spaceless %}
{{ macro.collection_item_widget(prototype) }}
{% endspaceless %}
{% endblock prototype_widget %}
{%extends'form\u div\u layout.html.twig%}
{%block collection_widget%}
{%import“ProjectSomeBundle:Macro:Macro.html.twig”作为宏%}
{%spaceless%}
{%如果定义了原型%}
{%set attr=attr | merge({'data-prototype':块('prototype_小部件')})%}
{%endif%}
{表格%中的行的%1}
{{macro.collection\u item\u小部件(行)}
{%endfor%}
{{form_rest(form)}
{%trans%}添加{%endtrans%}
{%endspaceless%}
{%endblock集合\小部件%}
{%block prototype_widget%}
{%spaceless%}
{{macro.collection\u item\u小部件(原型)}
{%endspaceless%}
{%endblock原型_widget%}
您可以注意到它使用宏,因此它是:
macro.html.twig
{% macro collection_item_widget(fields) %}
<li>
{% set fieldNum = 1 %}
{% for field in fields %}
<div class="field_{{ fieldNum }}">
{{ form_label(field) }}
{{ form_errors(field) }}
{{ form_widget(field) }}
</div>
{% set fieldNum = fieldNum + 1 %}
{% endfor %}
<button type="button" class="remove">{% trans %}Delete{% endtrans %}</button>
<div class="clear"></div>
</li>
{% endmacro %}
{%macro-collection\u-item\u小部件(字段)%}
{%set fieldNum=1%}
{字段%中的字段为%1}
{{form_标签(字段)}}
{{form_errors(field)}}
{{form_widget(field)}
{%set fieldNum=fieldNum+1%}
{%endfor%}
{%trans%}删除{%endtrans%}
{%endmacro%}
这是一个完整的示例,我希望您觉得它很有用,对您有用。我真的不能说您的问题在哪里,但应该在javascript和集合原型周围。此外,当您允许添加项目时,通常也允许删除 看看我的例子:
$builder->add('book', 'collection', array(
'type' => new BookType(),
'allow_add' => true,
'allow_delete' => true,
'prototype_name' => '__prototype__',
'by_reference' => false,
'error_bubbling' => false
);
对于渲染表单的模板,请包括以下内容:
{% block javascript %}
{{ parent() }}
{% javascripts
'@ProjectSomeBundle/Resources/public/js/form/collection.js'
%}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}
此外,您应该使用这些定制的集合小部件,并将其传递给twig。没什么特别的,它将被呈现为带有项目的无序列表,以使项目易于访问
config.yml:
twig:
form:
resources:
- 'ProjectSomeBundle:Form:fields.html.twig'
fields.html.twig:
{% extends 'form_div_layout.html.twig' %}
{% block collection_widget %}
{% import "ProjectSomeBundle:Macro:macro.html.twig" as macro %}
{% spaceless %}
<div class="collection">
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': block('prototype_widget') }) %}
{% endif %}
<div {{ block('widget_container_attributes') }}>
<ul>
{% for row in form %}
{{ macro.collection_item_widget(row) }}
{% endfor %}
</ul>
{{ form_rest(form) }}
</div>
<div class="clear"></div>
<button type="button" class="add">{% trans %}Add{% endtrans %}</button>
</div>
<div class="clear"></div>
{% endspaceless %}
{% endblock collection_widget %}
{% block prototype_widget %}
{% spaceless %}
{{ macro.collection_item_widget(prototype) }}
{% endspaceless %}
{% endblock prototype_widget %}
{%extends'form\u div\u layout.html.twig%}
{%block collection_widget%}
{%import“ProjectSomeBundle:Macro:Macro.html.twig”作为宏%}
{%spaceless%}
{%如果定义了原型%}
{%set attr=attr | merge({'data-prototype':块('prototype_小部件')})%}
{%endif%}
{表格%中的行的%1}
{{macro.collection\u item\u小部件(行)}
{%endfor%}
{{form_rest(form)}
{%trans%}添加{%endtrans%}
{%endspaceless%}
{%endblock集合\小部件%}
{%block prototype_widget%}
{%spaceless%}
{{macro.collection\u item\u小部件(原型)}
{%endspaceless%}
{%en