【问题标题】:Symfony rendering form only in first row of collectionSymfony 仅在集合的第一行呈现表单
【发布时间】:2018-11-12 19:29:19
【问题描述】:

也许有人可以帮助我,因为我一周以来一直在挖掘论坛和文档,如何呈现一个表格,其中我在第 3 列中有一个表格。

我有一个带有以下 sn-p 的 CollectionController 类:

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

附件表格如下所示:

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

我遇到的问题,表格只呈现在表格的第一行。谁能告诉我我做错了什么并且必须进行更改才能使其正常工作?

感谢和最好的问候, 迈克尔·奥布斯特

【问题讨论】:

    标签: php forms symfony html-table


    【解决方案1】:

    你的主要问题是,你不能多次分开这个 form_sn-p

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

    您的表格开始和表格结束必须包裹孔表格。如果您还希望在所有标签中都使用该表单,则不能仅在一个中编写起始表单 twig sn-p 。你必须写一些这样的代码:

    <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 标记。

    【讨论】:

    • 谢谢,我会试试的。我想我现在明白了这个问题。
    【解决方案2】:

    你应该看看如何在 symfony 文档中使用表单集合:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      相关资源
      最近更新 更多