简体   繁体   English

在一页中编辑所有实体对象的表格Symfony3

[英]Edit Forms of all entity's object in one page Symfony3

I want to have edit forms to all object in one view. 我想在一个视图中对所有对象进行编辑。

Already, I have this effect: 我已经有了这个效果: 图片

But when I try to edit a simple object, it doesn't work. 但是,当我尝试编辑一个简单的对象时,它不起作用。

Corrections.html.twig Corrections.html.twig

<div class="row">
    <div class="col-sm-10 col-sm-offset-1">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Nazwa poprawki</th>
                    <th>Status dla</th>
                    <th>Status dla klienta</th>
                    <th>Nazwa projektu</th>
                    <th>Klient</th>
                    <th>Obszar</th>
                    <th>Piorytet</th>
                    <th>Data utworzenia</th>
                    <th>Iteracja</th>
                </tr>
            </thead>
            <tbody>

                {% for correction in corrections %}
                    {{ form_start(form[loop.index0]) }}
                    <tr>
                        <td>{{correction.correctionName}}</td>
                        <td>{{ form_widget(form[loop.index0].adminStatusCorrectionId) }}</td>
                        <td>{{ form_widget(form[loop.index0].userStatusCorrectionId) }}</td>
                        <td>{{correction.projectId.projectName}}</td>
                        <td>{{correction.projectId.userId.firstName}} {{correction.projectId.userId.lastName}}</td>
                        <td>{{ form_widget(form[loop.index0].areaId) }}</td>
                        {% if correction.priority %}
                            <td>Tak</td>
                        {% else %}
                            <td>Nie</td>
                        {% endif %}
                        <td>{{correction.creationDate|date('Y-m-d')}}</td>
                        <td>{{correction.iteration}}</td>
                        <td>{{ form_widget(form[loop.index0].save) }}</td>
                    </tr>
                    </form>
                    {{ form_end(form[loop.index0]) }}
                {%endfor %}

            </tbody>
        </table>
    </div>
</div>

AdministratorController.php AdministratorController.php

public function correctionsAction(Request $request) {
    $repository = $this->getDoctrine()->getRepository('AppBundle:Correction');
    $corrections = $repository->findAll();

    foreach ($corrections as $key => $value) {
        $form = $this->createForm(CorrectionType::class, $corrections[$key]);
        $formView[] = $form->createView();
    }
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $correction = $form->getData();
        $em = $this->getDoctrine()->getManager();
        $em->persist($correction);
        $em->flush();

        return $this->redirectToRoute('admin_view_corrections');
    }

    return $this->render('administrator/corrections.html.twig', array(
                'corrections' => $corrections,
                'form' => $formView
    ));
}

CorrectionType.php CorrectionType.php

class CorrectionType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('adminStatusCorrectionId', EntityType::class, array(
                    'class' => 'AppBundle:AdminStatusCorrection',
                    'choice_label' => 'statusName'
                ))
                ->add('userStatusCorrectionId', EntityType::class, array(
                    'class' => 'AppBundle:UserStatusCorrection',
                    'choice_label' => 'statusName'
                ))
                ->add('areaId', EntityType::class, array(
                    'class' => 'AppBundle:Area',
                    'choice_label' => 'areaName'
                ))
                ->add('save', SubmitType::class, array('label' => 'Aktualizacja'))
        ;
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(array(
            'data_class' => Correction::class,
        ));
    }

}

What can I do now ? 我现在能做什么 ?

EDIT 编辑

All of my form have "correction" name. 我所有的表格都有“更正”名称。

In this case i have got 12 forms: 在这种情况下,我有12种形式:

<form name="correction" method="post"></form>

You should have the form handling inside your foreach cycle, also you need to give different name to each of your forms, so you could use the createNamed() method of the 'form.factory': 您应该在foreach周期内进行表单处理,还需要给每个表单使用不同的名称,因此可以使用“ form.factory”的createNamed()方法:

foreach ($corrections as $key => $value) {
    $formName = 'form_' . $key;
    $form = $this->get('form.factory')->createNamed($formName, CorrectionType::class, $corrections[$key]);

    if ($request->getMethod() === 'POST' && $request->request->has($formName)) {
        $form->handleRequest($request);
    }

    if ($form->isSubmitted() && $form->isValid()) {

        $correction = $form->getData();
        $em = $this->getDoctrine()->getManager();
        $em->persist($correction);
        $em->flush();

        return $this->redirectToRoute('admin_view_corrections');
    }

    $formView[] = $form->createView();
}

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

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