在一个页面Symfony3中编辑所有实体对象的表单

时间:2017-02-21 14:10:20

标签: php forms twig symfony

我希望在一个视图中为所有对象编辑表单。

我已经有了这个效果:PICTURE

但是当我尝试编辑一个简单的对象时,它不起作用。

Corrections.html.twig

app.delete('/post', function(req, res) {
    User.findOne({ _id: _id: ObjectId (req.user._id)}, function(err, result) {
        result.pull({ _id: ObjectId (req.body.post_id) });
    });
 });

AdministratorController.php

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

CorrectionType.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
    ));
}

我现在能做什么?

修改

我的所有表格都有“更正”的名称。

在这种情况下,我有12种形式:

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,
        ));
    }

}

1 个答案:

答案 0 :(得分:0)

您应该在foreach周期内处理表单,同时您需要为每个表单指定不同的名称,这样您就可以使用&#39; form.factory&#39;的

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