我一直在研究embedding上的Symfony 3教程,这是一个表单集合,我想将这个想法扩展到额外的嵌套级别。我环顾四周,对于Symfony 2有部分答案,但没有什么全面的(没有3)。
如果我们采取教程Task
有很多Tag
的例子,我将如何编码它,因此它延伸到:Task
有很多Tag
有很多SubTag
?
到目前为止,我认为我理解Form类:
任务:
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',
));
}
}
子标签:
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只能在单个级别上工作。
我能找到的最接近的工作代码是这个堆栈溢出答案here。但是,它似乎没有按照描述的方式工作,并且我无法确切地解决什么是错误的。
它与常规嵌入的表单集合没有任何不同。
但是,如果您想避免使用与父表单的原型字符串冲突的默认__NAME__
原型的问题,您应该采取选择TagType
和SubTag
类型的不同值。
来自the Symfony Docs entry on CollectionType
:
prototype_name
- type:string default:name
- 如果表单中有多个集合,或者更糟糕的是嵌套集合,则可能需要更改占位符,以便不使用相同的值替换不相关的占位符。
如果您想使用javascript抽象克隆操作,这可能非常有用,例如this article(粘贴在下面)中的那些,顺便说一句,这似乎是针对symfony3的!
例如,您可能希望包含传递给prototype_name
的相同值,作为集合持有者的html上的attr,以便您可以在replace
html上执行data-prototype
时动态访问它。
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);
}