Dar*_*one 5 javascript php forms prototype symfony
我一直在学习有关嵌入表单集合的 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 只能在单个级别上工作。
我能找到的最接近的工作代码是这里的堆栈溢出答案。然而,它似乎并不像描述的那样工作,而且我很难找出到底出了什么问题。
它与常规的嵌入式表单集合没有任何不同。
但是,如果您想避免默认__NAME__原型与父表单的原型字符串发生冲突,您应该采取 take 为TagType和SubTag类型选择不同的值。
原型名称
- 类型:字符串 默认值:名称
- 如果您的表单中有多个集合,或者更糟糕的是,有嵌套集合,您可能需要更改占位符,以便不相关的占位符不会被相同的值替换。
如果您想使用 javascript 抽象克隆操作,就像本文(粘贴在下面)中的操作一样,这会非常有帮助,顺便说一下,它似乎针对 symfony3!
例如,您可能希望包含传递给 的相同值prototype_name,作为集合持有者 html 上的 attr,以便在htmlreplace上执行 操作时可以动态访问它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);
}