javascript-Symfony 3多层嵌套表单
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-Symfony 3多层嵌套表单,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4172字,纯文字阅读大概需要6分钟。
内容图文
![javascript-Symfony 3多层嵌套表单](/upload/InfoBanner/zyjiaocheng/666/44a9ae0cfe54495eb14f0f358a4e3b4c.jpg)
我一直在研究关于embedding表单集合的Symfony 3教程,我想将其扩展到更多的嵌套级别.我环顾四周,Symfony 2的部分答案,但没有全面的答案(3没有).
如果我们使用教程“任务有很多标签”示例,我将如何编码它,以便扩展到:“任务有很多标签有很多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.但是,它似乎没有按所述方式工作,因此Im难以准确找出问题所在.
解决方法:
它与常规的嵌入式表单集合没有什么不同.
但是,如果要避免默认的__NAME__原型与父窗体的原型字符串冲突的麻烦,则应采取措施为TagType和SubTag类型选择不同的值.
从the Symfony Docs entry on CollectionType开始:
prototype_name
- type: string default: name
- If you have several collections in your form, or worse, nested collections you may want to change the placeholder so that unrelated placeholders are not replaced with the same value.
如果您想使用javascript抽象克隆动作,例如this article(粘贴在下面)中的动作,那么这可能非常有帮助,顺便说一下,这似乎是针对symfony3的!
例如,您可能希望包含传递给prototype_name的相同值,作为集合持有者html上的attr,以便在对数据原型html进行替换时可以动态访问它.
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);
}
内容总结
以上是互联网集市为您收集整理的javascript-Symfony 3多层嵌套表单全部内容,希望文章能够帮你解决javascript-Symfony 3多层嵌套表单所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。