Symfony 3:如何嵌入表单集合-提交后,数组中只有一个元素,而不是许多元素
大家好, 关于Symfony 3: 我试图遵循Symfony 3文档中“如何嵌入表单集合”的要求。它适用于他们首先提出的定义列表。但当我尝试下一步:允许原型使用新标记时,它只返回我的最后一个嵌入表单。 所以我知道我的实体和EntityType一样有效。错误一定在树枝上。 提前感谢您的帮助! 它适用于他们在第一部分中提出的定义列表。 因此,我知道我的实体与EntityType一样有效。Symfony 3:如何嵌入表单集合-提交后,数组中只有一个元素,而不是许多元素,symfony,symfony-forms,Symfony,Symfony Forms,大家好, 关于Symfony 3: 我试图遵循Symfony 3文档中“如何嵌入表单集合”的要求。它适用于他们首先提出的定义列表。但当我尝试下一步:允许原型使用新标记时,它只返回我的最后一个嵌入表单。 所以我知道我的实体和EntityType一样有效。错误一定在树枝上。 提前感谢您的帮助! 它适用于他们在第一部分中提出的定义列表。 因此,我知道我的实体与EntityType一样有效。 enter code here {% extends "@App/baseAdmin.html.twig" %}
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 %}
)
您刚刚对JS代码的一部分进行了注释
newForm = newForm.replace(/__name__/g, index);
取消对此的注释,一切都会很好我的行是:
newForm=newForm.replace(/名称标签/g,'Spectateur n°'+索引)
我的错误是,我认为我可以将其更改为文本标签:
太好了,就是这个!非常感谢,你救了我!现在它工作得很好 你能给我们看看实体吗
newForm = newForm.replace(/__name__/g, index);