我一直在研究关于嵌入 (一组表单)的Symfony 3教程,我想将这个想法扩展到额外的嵌套级别。我环顾四周,找到了Symfony 2的部分答案,但没有全面的答案(3没有)。
如果我们学习教程Task
有许多Tag
示例,我将如何编写它的代码以便扩展到:Task
有很多Tag
有很多SubTag
?
到目前为止,我认为我理解表单类:
任务:
class TaskType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('description');
$builder->add('tags', CollectionType::class, array(
'entry_type' => TagType::class,
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Task',
));
}
}
标签:
class TagType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('sub_tags', CollectionType::class, array(
'entry_type' => SubTagType::class,
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Tag',
));
}
}
SubTag:
class SubTagType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\SubTag',
));
}
}
和基本的Twig类:
{{ form_start(form) }}
{# render the task's only field: description #}
{{ form_row(form.description) }}
<h3>Tags</h3>
<ul class="tags">
{# iterate over each existing tag and render its only field: name #}
{% for tag in form.tags %}
<li>{{ form_row(tag.name) }}</li>
{% for sub_tag in tag.sub_tags %}
<li>{{ form_row(sub_tag.name) }}</li>
{% endfor %}
{% endfor %}
</ul>
{{ form_end(form) }}
但在这一点上,我不确定原型和javascript将如何工作。有人能解释一下我下一步该怎么做吗?这就是正确的方法吗?
我的第一个想法是,如果我们要进行额外的级别,那么将JS推广到任意数量的级别可能是明智的,因为本教程使用的JS非常多,只能在单个级别上工作。
我能找到的最接近的工作代码是这个堆栈溢出响应这里。然而,它似乎不像描述的那样起作用,而且我很难准确地知道出了什么问题。
发布于 2016-10-19 19:32:06
它与普通的嵌入式表单集合没有什么不同。
但是,如果您想避免与父窗体的原型字符串发生冲突的默认__NAME__
原型的麻烦,那么您应该考虑为TagType
和SubTag
类型选择不同的值。
来自CollectionType
prototype_name
如果您想用javascript抽象您的克隆操作,比如这篇文章 (粘贴在下面)中的操作,顺便说一句,这些操作似乎是针对symfony3的,这可能非常有帮助!
例如,您可能希望将传递给prototype_name
的相同值包含在集合持有者的html上,以便在对data-prototype
html执行replace
时能够动态地访问它。
var $collectionHolder;
// setup an "add a tag" link
var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
var $newLinkLi = $('<li></li>').append($addTagLink);
jQuery(document).ready(function() {
// Get the ul that holds the collection of tags
$collectionHolder = $('ul.tags');
// 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);
$addTagLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new tag form (see next code block)
addTagForm($collectionHolder, $newLinkLi);
});
function addTagForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');
// get the new index
var index = $collectionHolder.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.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);
}
https://stackoverflow.com/questions/40138645
复制相似问题