代码之家  ›  专栏  ›  技术社区  ›  Guillaume Harari

symfony中有多种形式。显示有效,但多个持久

  •  0
  • Guillaume Harari  · 技术社区  · 6 年前

    我有一页的表格是隐藏式的。 表单来自具有不同ID的同一实体。 显示模态工程。对于我想要显示的每个模式,输入中的数据都是正常的。 问题是:当我提交表单时,所有表单(包括隐藏表单)都将被持久化。

    //EditServiceType.php
    
    namespace Guillaume\PartnerManagerBundle\Form;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\Extension\Core\Type\SubmitType;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
    
    class EditServiceType extends AbstractType
    {
        /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name', null, array(
                    'label_attr' => array('class' => 'col-form-label'),
                    'attr'       => array('class' => 'form-control')
                ))
                ->add('type')
                ->add('version')
                ->add('connection_type', ChoiceType::class, array(
                    'choices'   => array(
                    'Connected' => 2,
                    'Local'    => 1,
                )));
    
        }/**
         * {@inheritdoc}
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'Guillaume\PartnerManagerBundle\Entity\Service'
            ));
        }
    
        /**
         * {@inheritdoc}
         */
        public function getBlockPrefix()
        {
            return 'edit_guillaume_partnermanagerbundle_service';
        }
    
    
    }
    

    控制器的一部分

    $formEdits = [];
            $forms = [];
            $serviceFormEdits = [];
            foreach($services as $service) {
                $serviceFormEdits[$service->getId()] = $em->getRepository('GuillaumePartnerManagerBundle:Service')->find($service);
                $forms[$service->getId()]   = $this->createForm('Guillaume\PartnerManagerBundle\Form\EditServiceType', $serviceFormEdits[$service->getId()]);
                $formEdits[$service->getId()] = $forms[$service->getId()]->createView();
    
                $forms[$service->getId()]->handleRequest($request);
                if ($forms[$service->getId()]->isSubmitted() && $forms[$service->getId()]->isValid()) {
    
                        $em->persist($serviceFormEdits[$service->getId()]);
                        $em->flush();
                        $this->addFlash('notice', 'Service has been edited');
                    }
                }
            }
    //(...)
    
    return $this->render('GuillaumePartnerManagerBundle:customers:customer.html.twig', [
    'formEdits'           => $formEdits,
    ]);
    

    我树枝的一部分(模态)

    {% for result in results %}
            {% form_theme formEdits[result.service.id] 'bootstrap_4_horizontal_layout.html.twig' %}
            <div class="modal fade" id="editEnvironment-{{ result.service.id }}" tabindex="-1" role="dialog"
        aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
              <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <h2 class="modal-title" id="exampleModalLongTitle">{{ 'Edit Environment'|trans }}</h2>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                     {{ form_start(formEdits[result.service.id]) }} 
                     <div class="form-group row">
                        {{ form_widget(formEdits[result.service.id]) }} 
                     </div>
                     <div class="modal-footer row">
                         <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" value="cancel" class="btn btn-secondary" data-dismiss="modal">{{ 'Cancel'|trans }}</button>
                            <button type="submit" value="save" class="btn btn-primary">{{ 'Validate'|trans }}</button>
                        </div>
                     </div>
                     {{ form_end(formEdits[result.service.id]) }}
                  </div>
                </div>
              </div>
            </div>
            {% endfor %}
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Guillaume Harari    6 年前

    我终于明白了。

    正在选项冲突解决程序中添加“id”

    $forms[$service->getId()]   = $this->createForm('Guillaume\PartnerManagerBundle\Form\EditServiceType', $serviceFormEdits[$service->getId()], ['id' => $service->getId()]);
    

    添加隐藏在FormType中的输入

        ->add('id', HiddenType::class, array(
            'data' => $options['id'],
            'mapped' => false,
        ))
    

    提交有效时使用此ID

                $postData = $request->request->get('edit_guillaume_partnermanagerbundle_service');
                $postId = $postData['id'];
                $forms[$postId]->handleRequest($request);
    
                if ($forms[$postId]->isSubmitted() && $forms[$postId]->isValid()) {
    
                            $em->persist($serviceFormEdits[$postId]);
                            $em->flush($serviceFormEdits[$postId]);
    //(...)