簡體   English   中英

Symfony渲染形式僅在集合的第一行中

[英]Symfony rendering form only in first row of collection

也許有人可以幫我,因為一個星期以來我一直在挖掘論壇和文檔,如何在第3列中的表格中呈現表格。

我有一個帶有以下代碼段的CollectionController類:

return $this->render(
        'vollmachtapp/pages/collections/list2.html.twig',
        [
            'attachmentForm' => $attachmentForm->createView(),
            'list' => $apiClient->listAllVollmachten()
        ]
    );

attachmentForm看起來像這樣:

$attachmentForm = $this->createForm( SimpleAttachmentType::class, $attachment );

這是我的SimpleAttachmentType:

class SimpleAttachmentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
        ->add('file', FileType::class)
        ->add('vollmachtId', HiddenType::class)
        ->add('save', SubmitType::class, ['label' => 'Vollmacht hochladen']);
  }
}

現在,我使用的樹枝如下所示:

<table id="datatable" class="table table-striped table-bordered">
                    <thead>
                    <tr>
                        <th>Vollmacht ID</th>
                        <th>Users</th>
                        <th>Upload/Download</th>
                    </tr>
                    </thead>

                    <tbody>

                        {% for collection in list.items %}
                            <tr>
                                <td>{{ collection.id }}</td>
                                <td>
                                    {% for user in collection.users %}
                                        <a href="{{ url('user_detail', {'id': user.id}) }}">
                                            {{ user.name }}
                                        </a><br>
                                    {% endfor %}
                                </td>
                                <td>
                                    <a href="{{ url('downloadVollmacht', {'id': user.id}) }}"><button type="button" class="btn btn-sm btn-primary">Download Vollmacht</button></a>
                                    {% if(app.user.role == "admin") %}
                                    <hr>
                                    <div class="text-center mtop20">
                                        {{ form_start(attachmentForm, {method: 'POST'}) }}
                                            {% if not attachmentForm.vars.valid %}
                                                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
                                                    {{ form_errors(attachmentForm.file) }}
                                                </div>
                                            {% endif %}
                                            {{ form_widget(attachmentForm.vollmachtId, {'name': vollmachtId, 'value': collection.id}) }}
                                            {{ form_widget(attachmentForm.file) }}
                                            <br />

                                            {{ form_widget(attachmentForm.save, {attr: {'class': 'btn-left btn btn-sm btn-primary'}}) }}
                                        {{ form_end(attachmentForm) }}
                                    </div>
                                    {% endif %}
                                </td>
                            </tr>
                        {% endfor %}

                    </tbody>
                </table>

我的問題是,表單僅在表格的第一行中呈現。 誰能告訴我我做錯了什么,必須進行更改才能使其正常工作?

謝謝,最好的問候,邁克爾·奧伯斯特

您的主要問題是,您不能多次分離此form_snippet

{{ form_start(attachmentForm, {method: 'POST'}) }}

您的表格開始和表格結束必須包裹孔表格。 如果您還想在所有標簽中使用該表格,則不能只用一張寫起始表格的樹枝片段。 您必須編寫如下代碼:

<table id="datatable" class="table table-striped table-bordered">
                <thead>
                <tr>
                    <th>Vollmacht ID</th>
                    <th>Users</th>
                    <th>Upload/Download</th>
                </tr>
                </thead>

                <tbody>                 
                        {% for collection in list.items %}
                            <tr>
                                {{ form_start(attachmentForm, {method: 'POST'}) }}
                                <td>{{ collection.id }}</td>
                                <td>
                                    {% for user in collection.users %}
                                        <a href="{{ url('user_detail', {'id': user.id}) }}">
                                            {{ user.name }}
                                        </a><br>
                                    {% endfor %}
                                </td>
                                <td>
                                    <a href="{{ url('downloadVollmacht', {'id': user.id}) }}"><button type="button" class="btn btn-sm btn-primary">Download Vollmacht</button></a>
                                    {% if(app.user.role == "admin") %}
                                    <hr>
                                    <div class="text-center mtop20">

                                            {% if not attachmentForm.vars.valid %}
                                                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
                                                    {{ form_errors(attachmentForm.file) }}
                                                </div>
                                            {% endif %}
                                            {{ form_widget(attachmentForm.vollmachtId, {'name': vollmachtId, 'value': collection.id}) }}
                                            {{ form_widget(attachmentForm.file) }}
                                            <br />

                                            {{ form_widget(attachmentForm.save, {attr: {'class': 'btn-left btn btn-sm btn-primary'}}) }}

                                    </div>
                                    {% endif %}
                                </td>
                                {{ form_end(attachmentForm) }}
                            </tr>                               
                        {% endfor %}
                </tbody>
            </table>

但是也許它可以稍微重構您的HTML標記。

您應該看看如何使用symfony文檔中的一系列表單:

https://symfony.com/doc/current/reference/forms/types/collection.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM