symfony - Symfony 3:如何嵌入表单集合 - 提交后我在数组中只有一个元素而不是很多
问题描述
大家好,在 Symfony 3 上:我尝试遵循 Symfony 3 文档中“如何嵌入表单集合”的要求。它适用于他们首先提出的定义列表。但是当我尝试下一步时:允许带有原型的新标签,它只返回我最后一个嵌入表单。所以我知道我的 Entity 和 EntityType 都有效。错误必须在 Twig 上。在此先感谢您的帮助!它适用于他们在第一部分中提出的定义列表。所以我知道我的 Entity 和 EntityType 都有效。
enter code here
{% extends "@App/baseAdmin.html.twig" %} .
{% block contenu %} .
{#{{ dump(formreservation) }}#} .
{#{{ form(formreservation) }}#} .
<div> .
Date : {{ "now"|date("d/m/Y") }} .
{{ form_start(formreservation, {'attr': {'class': 'form'}}) }} .
{#{{ dump(formreservation) }}#} .
<p> .
{#retourne message erreur si besoin après méthode isValid dans Controleur#} .
{{ form_errors(formreservation.spectacle) }} .
{{ form_label(formreservation.spectacle, null,
{'label_attr': {'class': 'form-label'}}) }} :
{{ form_widget(formreservation.spectacle, {'attr':
{'class': 'form-control'}}) }} .
</p>
<p>
{{ form_label(formreservation.spectateur, null,
{'label_attr': {'class': 'form-label'}}) }}
<ul class="spectateur" data-prototype="{{ form_widget(formreservation.spectateur.vars.prototype)|e('html_attr') }}">
</ul>
</p>
<p>
{#retourne message erreur si besoin après méthode isValid dans Controleur#}
{{ form_errors(formreservation.client) }}
{{ form_label(formreservation.client, null,
{'label_attr': {'class': 'form-label'}}) }} :
{{ form_widget(formreservation.client, {'attr':
{'class': 'form-control'}}) }}
</p>
{# génération du champ CSRF - _token# (Cross Site Request Forgeries en champ caché #}
{{ form_rest(formreservation) }}
{{ form_end(formreservation) }}
</div>
{# Partie JavaScript #}
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous">
</script>
<script>
var $collectionHolder;
// setup an "add a tag" link
var $addTagButton = $('<button type="button" class="add_tag_link">Ajoutez un spectateur</button>');
var $newLinkLi = $('<li></li>').append($addTagButton);
jQuery(document).ready(function() {
// Get the ul that holds the collection of tags
var $collectionHolder = $('ul.spectateur');
// add a delete link to all of the existing tag form li elements
//inutile pour le moment, ajoute un bouton qui créé la confusion
/* $collectionHolder.find('li').each(function() {
addTagFormDeleteLink($(this));
});*/
// add the "add a tag" anchor and li to the tags ul
$collectionHolder.append($newLinkLi);
// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);
$addTagButton.on('click', function(e) {
// add a new tag form (see next code block)
e.preventDefault();
addTagForm($collectionHolder, $newLinkLi);
});
});
function addTagForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
//console.log($collectionHolder);
var prototype = $collectionHolder.data('prototype');
//console
// get the new index
var index = $collectionHolder.data('index');
var newForm = prototype;
// You need this only if you didn't set 'label' => false in your tags field in TaskType
// Replace '__name__label__' in the prototype's HTML to
// instead be a number based on how many items we have
newForm = newForm.replace(/__name__label__/g, 'Spectateur n° '+ index);
//newForm = newForm.replace(/__name__/g, index);
// increase the index with one for the next item
$collectionHolder.data('index', index + 1 );
// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
// add a delete link to the new form
addTagFormDeleteLink($newFormLi);
}
function addTagFormDeleteLink($tagFormLi) {
var $removeFormButton = $('<button type="button">enlever ce spectateur</button><br>');
$tagFormLi.append($removeFormButton);
$removeFormButton.on('click', function(e) {
// remove the li for the tag form
e.preventDefault();
$tagFormLi.remove();
});
}
</script>
{% endblock %}
预期结果:包含所有嵌入表单的集合。实际上它只返回嵌入表单中的最后一个元素。我确实检查了实体,它没有使用我添加到实体的“添加”方法。
这里是 ReservationType 构建器的添加部分:
->add('spectateurs', CollectionType::class, [
'entry_type' => SpectateurReservationType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
] .
) .
解决方案
)
你刚刚注释了一部分 JS 代码
newForm = newForm.replace(/__name__/g, index);
取消注释这个,任何事情都会很好
推荐阅读
- android - 在 MPAndroidChart 中的活动之间传递数据集
- cordova - 如何在cordova phonegap中为Windows应用程序设置固定窗口大小
- python - 从网页中的表格获取链接
- python - 如何强制 pandas .loc 返回系列
- javascript - React Redux 操作未调度但请求成功
- wso2 - 在 DAS 中部署 ESB 分析车
- angular - Angular PWA 离线数据处理
- c++ - 我包含哪个标头(cstddef、cstdio、cstdlib 等)以获得 size_t 的定义是否重要?
- docker - Docker 错误:没有这样的容器:friendlyhello
- php - 检测虚拟机 ovf 中所做的更改