简体   繁体   中英

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

<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

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

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:

<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 ($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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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