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

コードをtwigファイルに配置すると、何らかの理由で

    <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 ie に

    <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>

プロトタイプはnullではなく、すべて順調です

于 2012-10-01T22:35:00.177 に答える