【问题标题】:Symfony 3 Multiple Level Nested FormsSymfony 3 多级嵌套表单
【发布时间】:2017-03-01 12:31:37
【问题描述】:

我一直在学习关于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。但是,它似乎不像描述的那样工作,而且我很难弄清楚到底出了什么问题。

【问题讨论】:

  • 嘿 - 如果您遇到了我无法在回答中解决的特定问题,请告诉我。 (我看到您曾一度将其标记为“已接受”。)
  • 由于某种原因不被接受!

标签: javascript php forms symfony prototype


【解决方案1】:

它与常规的嵌入式表单集合没有任何不同。

但是,如果您想避免默认的__NAME__ 原型与父表单的原型字符串发生冲突,您应该为TagType 和SubTag 类型选择不同的值。

来自the Symfony Docs entry on CollectionType:

原型名称

  • 类型:字符串 默认值:名称
  • 如果您的表单中有多个集合,或更糟嵌套集合,您可能需要更改占位符,以便不相关的占位符不会被相同的值替换。

如果您想使用 javascript 抽象克隆操作,这将非常有用,例如 this article(粘贴在下面)中的那些,顺便说一下,它似乎针对 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);
}

【讨论】:

  • 哈——我能做些什么来赢得你的青睐?
  • @Cameron,你还在吗?我正面临您正在解决的问题。我不确定您建议如何编写 JS 代码。如何编写“添加子标签”部分?谢谢
  • 嘿,@mario - 我还在这里。如果您可以将问题缩小到更具体的问题,我们很乐意提供帮助!
  • 嘿@Cameron,感谢您的回复。在示例中,您如何编写 JS 来插入/克隆子标签的表单?我能够为多个标签编写一个表单,为多个子标签编写一个表单;我不是要编写一个独特的表单,您可以在其中创建更多标签,并在每个子标签中创建更多子标签。我的理解是你需要混合你的代码,但我不确定如何。 (我看了几个小时,有时比星期四晚)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多