Форма рендеринга Symfony только в первом ряду коллекции - PullRequest
0 голосов
/ 12 ноября 2018

возможно, кто-то может мне помочь, потому что я с неделю копаю форумы и документацию, как сделать таблицу, где у меня есть форма в 3-м столбце.

У меня есть класс CollectionController со следующим фрагментом:

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

AttachForm выглядит следующим образом:

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

У меня проблема, форма отображается только в 1-й строке таблицы. Кто-нибудь может сказать мне, что я делаю не так, и нужно изменить, чтобы это заработало?

Спасибо и всего наилучшего, Майкл Обстер

Ответы [ 2 ]

0 голосов
/ 12 ноября 2018

Ваша главная проблема в том, что вы не можете разделить этот form_snippet несколько раз

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

Ваша форма Start и форма End должны быть завернуты в форму отверстия. если вы хотите эту форму также во всех тегах, вы не можете написать фрагмент ветки начальной формы только в одном. Вы должны написать такой код:

<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-разметку.

0 голосов
/ 12 ноября 2018

Посмотрите, как работать с коллекцией форм в Symfony. Документация:

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...