Symfony2 - 动态生成的表单在编辑表单时不起作用

时间:2016-10-24 12:32:39

标签: symfony

基于文档:http://symfony.com/doc/2.8/form/dynamic_form_modification.html#form-events-submitted-data

我准备了动态生成的表单。一切正常但只有当我使用表单添加新数据(/ new)时,我使用相同的表格来编辑现有数据 - 不工作

  1. “约会”的简单形式。它应该是这样的:用户选择客户端,然后第二个“选择”正在填充正确的数据 - 取决于首先选择的每个客户端。这工作正常,但只有当我尝试添加新的约会。当我尝试编辑编号
  2. 
    class AppointmentType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name')
                ->add('client', EntityType::class, array(
                    'class'       => 'SystemAdminBundle:Client',
                    'placeholder' => '',
                ));
            $formModifier = function(\Symfony\Component\Form\FormInterface $form, Client $client) 
            {
                $diseases = array();
                if($client !== null) {
                    $diseases = $client->getDiseases();
                }
                $form->add('disease', EntityType::class, array(
                        'class'       => 'SystemAdminBundle:Disease',
                        'placeholder' => '',
                        'choices'     => $diseases,
                    ));
            };
            $builder->addEventListener(
                FormEvents::PRE_SET_DATA,
                function (FormEvent $event) use ($formModifier) {
    
                    $data = $event->getData();
    
                    $formModifier($event->getForm(), $data->getClient());
    
                }
            );
    
            $builder->get('client')->addEventListener(
                FormEvents::POST_SUBMIT,
                function (FormEvent $event) use ($formModifier) {
    
                    $client = $event->getForm()->getData();
    
                    $formModifier($event->getForm()->getParent(), $client);
                }
            );
    
        }
    
    
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'System\AdminBundle\Entity\Appointment'
            ));
        }
    }
    
    1. 约会控制器 - 这是添加新约会和编辑的功能。对于“新”,我的代码适用于“编辑”号。
    2.     public function newAction(Request $request)
          {
              $appointment = new Appointment();
              $form = $this->createForm(AppointmentType::class, $appointment);
              $form->handleRequest($request);
      
              if ($form->isSubmitted() && $form->isValid()) {
      
      
                  $data = $request->request->get('appointment');
      
                  if(array_key_exists('name', $data)) {
      
                      $em = $this->getDoctrine()->getManager();
                      $em->persist($appointment);
                      $em->flush();
      
                      return $this->redirectToRoute('appointment_show', array('id' => $appointment->getId()));
      
                  }
      
              }
      
              return $this->render('appointment/new.html.twig', array(
                  'appointment' => $appointment,
                  'form' => $form->createView(),
              ));
      
          }
          public function editAction(Request $request, Appointment $appointment)
          {
              $deleteForm = $this->createDeleteForm($appointment);
              $appointment = new Appointment();
              $editForm = $this->createForm('System\AdminBundle\Form\AppointmentType', $appointment);
              $editForm->handleRequest($request);
      
              if ($editForm->isSubmitted() && $editForm->isValid()) {
      
                  $data = $request->request->get('appointment');
      
                  if(array_key_exists('name', $data)) {
      
                      $em = $this->getDoctrine()->getManager();
                      $em->persist($appointment);
                      $em->flush();
      
                      return $this->redirectToRoute('appointment_show', array('id' => $appointment->getId()));
      
                  }
      
                  return $this->redirectToRoute('appointment_edit', array('id' => $appointment->getId()));
              }
      
              return $this->render('appointment/edit.html.twig', array(
                  'appointment' => $appointment,
                  'edit_form' => $editForm->createView(),
                  'delete_form' => $deleteForm->createView(),
              ));
          }
      
      
      1. 查看“新”约会
      2. 
            {% block content %}
        
            {{ form_start(form) }}
                {{ form_widget(form) }}
                
            {{ form_end(form) }}
        
            
            window.onload = function() {
        
            var $sport = $('#appointment_client');
        
            $sport.change(function() {
        
              var $form = $(this).closest('form');
        
              var data = {};
              data[$sport.attr('name')] = $sport.val();
              data['appointment[_token]'] = $('#appointment__token').val();
        
              $.ajax({
                url : $form.attr('action'),
                type: $form.attr('method'),
                data : data,
                success: function(html) {
        
                  $('#appointment_disease').replaceWith(
        
                    $(html).find('#appointment_disease')
                  );
        
                }
              });
            });
        
            };
            
            {% endblock %}
            
        1. 查看“编辑”约会 - 它与“新”约会
        2. 几乎相同
          
              {% block content %}
              {{ form_start(edit_form) }}
                  {{ form_widget(edit_form) }}
                  
              {{ form_end(edit_form) }}
          
              
              window.onload = function() {
          
          
              var $sport = $('#appointment_client');
          
              $sport.change(function() {
          
                var $form = $(this).closest('form');
          
                var data = {};
                data[$sport.attr('name')] = $sport.val();
                data['appointment[_token]'] = $('#appointment__token').val();
          
                $.ajax({
                  url : $form.attr('action'),
                  type: $form.attr('method'),
                  data : data,
                  success: function(html) {
          
                  $('#appointment_disease').replaceWith(
          
                  $(html).find('#appointment_disease')
              );   
              }
              });
              });
          
              };
              
              {% endblock %}
          
          

1 个答案:

答案 0 :(得分:0)

您在mutate中创建了new Appointment,然后将其保留。您应该使用函数参数中的那个,处理请求并刷新,因为您的对象已经存在。

所以删除这些行:

editAction