2

我正在尝试在 symfony2 的子表单中嵌入多个文件上传。我有一个“想法”表单,我想在其中呈现多个“文档”表单。我已经按照文档进行操作,但是当数据原型在表单中显示为 Null 时,我就停止了。据我了解,原型是呈现表单类型的 html 字符串。我已经搜索过,似乎我是唯一一个遇到这个问题的人。它一定很小,但任何帮助将不胜感激。我的表格类型:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
        ->add('title', 'text')
       ->add('category', 'entity', 
        array('class' => 'AcmeIdeaBundle:Category',
        'property' => 'name', 
        ))
        ->add('description', 'textarea')
        ->add('file','file')
        ->add('video')
        ->add('documents', 'collection', array('type' => new DocumentType,
        'allow_add' => true,
        'by_reference' => false,
        'allow_delete' => true,
        'prototype' => true));
         }

文档表单类型生成器:

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
        $builder
        ->add('file');
}

相关树枝文件:

    {% block body %}
    <form action="{{ path('idea_create') }}" method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}
    <ul class="docs" data-prototype="{{ form_widget(form.documents.vars.prototype)|e }} " >
    {% for document in form.documents %}
    <li> {{ form_row(document.file) }} </li>
    {% endfor %}

    </ul>

如果我在控制器中硬编码一些文档对象,它们看起来很好并且可编辑,但我需要它是动态的。

javascript代码:

    var collectionHolder = $('ul.docs');

    var $addDocumentLink = $('<a href="#" class="add_documents_link">Add a picture</a>');
    var $newLinkLi = $('<li></li>').append($addDocumentLink);

    jQuery(document).ready(function(){
    collectionHolder.find('li').each(function(){
    addDocumentFormDeleteLink($(this));
    });

    collectionHolder.append($newLinkLi);

    $addDocumentLink.on('click', function(e) {
    e.preventDefault();

    addDocumentForm(collectionHolder, $newLinkLi);
    });
    });

    function addDocumentForm(collectionHolder, $newLinkLi) {

    var prototype = collectionHolder.attr('data-prototype');

    var newForm = prototype.replace(/\$\$name\$\$/g, collectionHolder.children().length);

    var $newFormLi = $('<li></li>').append(newForm);

    $newLinkLi.before($newFormLi);
    addDocumentFormDeleteLink($newFormLi);
    }

    function addDocumentFormDeleteLink($docFormLi) {
    var $removeFormA = $('<a href= "#">remove?</a>');
    $docFormLi.append($removeFormA);

    $removeFormA.on('click', function(e) {
    e.preventDefault();
    $docFormLi.remove();
    });
    }

这也是我第一次在这里提问,所以如果我做错了什么,我深表歉意。

4

1 回答 1

0

出于某种原因,当我将代码放在树枝文件中时

    <ul class="docs" data-prototype="{{ form_widget(form.documents.vars.prototype)|e }} " >
    {% for document in form.documents %}
    <li> {{ form_row(document.file) }} </li>
    {% endfor %}

进入选项卡式 div 即

    <div class="tab-pane" id="details"> 
    {{ form_row(form.video) }}
    <ul class="docs" data-prototype="{{ form_widget(form.documents.vars.prototype)|e }} " >
    {% for document in form.documents %}
    <li> {{ form_row(document.path) }} </li>
    {% endfor %}
    </ul>
    {{ form_row(form.description) }}
    </div>

现在原型不为空,一切都很好

于 2012-10-01T22:35:00.177 回答