简体   繁体   English

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

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

perhaps somebody can help me because I'm digging forums and documentation since a week how to render a table where I have a form in 3rd column. 也许有人可以帮我,因为一个星期以来我一直在挖掘论坛和文档,如何在第3列中的表格中呈现表格。

I have a class CollectionController with following snippet: 我有一个带有以下代码段的CollectionController类:

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

The attachmentForm looks like this: attachmentForm看起来像这样:

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

Here is my SimpleAttachmentType: 这是我的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']);
  }
}

Now I'm using a twig which looks like: 现在,我使用的树枝如下所示:

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

The problem I have, the form is only rendered in the 1st row of the table. 我的问题是,表单仅在表格的第一行中呈现。 Can anybody tell me what I'm doing wrong and have to change to get it work? 谁能告诉我我做错了什么,必须进行更改才能使其正常工作?

Thanks and best regards, Michael Obster 谢谢,最好的问候,迈克尔·奥伯斯特

Your Main Problem is, that you cant seperate this form_snippet multiple times 您的主要问题是,您不能多次分离此form_snippet

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

Your form Start and form End have to be wrapping the hole form. 您的表格开始和表格结束必须包裹孔表格。 if your want that form also in all tags you cant write the start form twig snippet only in one . 如果您还想在所有标签中使用该表格,则不能只用一张写起始表格的树枝片段。 You have to write some code like that: 您必须编写如下代码:

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

But maybe its reconstruct your HTML markup a lil bit. 但是也许它可以稍微重构您的HTML标记。

You should take a look at how to work with a collection of forms in symfony Documentation : 您应该看看如何使用symfony文档中的一系列表单:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM