Symfony2-编辑表单时动态生成的表单不工作

Symfony2-编辑表单时动态生成的表单不工作,symfony,Symfony,根据文件: 我准备了动态生成表单。一切都正常,但只有当我使用表单添加新数据(/new)时,当我使用相同的表单编辑现有数据时,才正常工作-不工作 “预约”的简单表格。它应该是这样工作的:用户选择客户端,然后第二个“选择”填充正确的数据-取决于第一个选择的每个客户端。这可以,但只有当我尝试添加新约会时。当我尝试编辑否 类AppointType扩展了AbstractType { 公共函数buildForm(FormBuilderInterface$builder、array$options) { $b

根据文件:

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

  • “预约”的简单表格。它应该是这样工作的:用户选择客户端,然后第二个“选择”填充正确的数据-取决于第一个选择的每个客户端。这可以,但只有当我尝试添加新约会时。当我尝试编辑否
  • 类AppointType扩展了AbstractType { 公共函数buildForm(FormBuilderInterface$builder、array$options) { $builder ->添加('名称') ->添加('client',EntityType::类,数组( 'class'=>'SystemAdminBundle:Client', '占位符'=>'', )); $formModifier=函数(\Symfony\Component\Form\FormInterface$Form,Client$Client) { $diseases=array(); 如果($client!==null){ $diseases=$client->getDiseases(); } $form->add('disease',EntityType::class,array( 'class'=>'SystemAdminBundle:Disease', '占位符'=>'', “选择”=>$疾病, )); }; $builder->addEventListener( FormEvents::预设置数据, 函数(FormEvent$event)使用($formModifier){ $data=$event->getData(); $formModifier($event->getForm(),$data->getClient()); } ); $builder->get('client')->addEventListener( FormEvents::POST_提交, 函数(FormEvent$event)使用($formModifier){ $client=$event->getForm()->getData(); $formModifier($event->getForm()->getParent(),$client); } ); } 公共函数配置选项(选项解析器$resolver) { $resolver->setDefaults(数组( “数据\u类”=>“系统\AdminBundle\Entity\Appointment” )); } }
  • 约会控制器-这里是添加新约会和编辑的功能。对于“新建”我的代码有效,对于“编辑”否
  • 公共函数newAction(请求$Request) { $appointment=新约会(); $form=$this->createForm(AppointmentType::class,$appointment); $form->handleRequest($request); 如果($form->isSubmitted()&&&$form->isValid()){ $data=$request->request->get(‘约会’); 如果(数组\键\存在('name',$data)){ $em=$this->getDoctrine()->getManager(); $em->persist($appoint); $em->flush(); 返回$this->redirectToRoute('appointment_show',array('id'=>$appointment->getId()); } } 返回$this->render('appointment/new.html.twig',数组( “约会”=>$appointment, 'form'=>$form->createView(), )); } 公共功能编辑操作(请求$Request,约会$Appointment) { $deleteForm=$this->createDeleteForm($appointment); $appointment=新约会(); $editForm=$this->createForm('System\AdminBundle\Form\AppointmentType',$appointment); $editForm->handleRequest($request); 如果($editForm->isSubmitted()&&&$editForm->isValid()){ $data=$request->request->get(‘约会’); 如果(数组\键\存在('name',$data)){ $em=$this->getDoctrine()->getManager(); $em->persist($appoint); $em->flush(); 返回$this->redirectToRoute('appointment_show',array('id'=>$appointment->getId()); } 返回$this->redirectToRoute('appointment_edit',array('id'=>$appointment->getId()); } 返回$this->render('appointment/edit.html.twig',数组( “约会”=>$appointment, “编辑表单”=>$editForm->createView(), 'delete_form'=>$deleteForm->createView(), )); }
  • “新”约会的视图
  • {%block content%} {{form_start(form)}} {{form_widget(form)} {{form_end(form)}} window.onload=函数(){ 变量$sport=$(“#预约#客户”); $sport.change(函数(){ var$form=$(this).closest('form'); 变量数据={}; 数据[$sport.attr('name')]=$sport.val(); 数据['appointment[_token]]=$('appointment_token').val(); $.ajax({ url:$form.attr('action'), 类型:$form.attr('method'), 数据:数据, 成功:函数(html){ $(“#预约"疾病”)。替换为( $(html).查找(“#约会"疾病”) ); } }); }); }; {%endblock%}
  • “编辑”约会的视图-与“新”约会的视图几乎相同
  • {%block content%} {{form_start(edit_form)} {{表单小部件(编辑表单)} {{form_end(edit_form)} window.onload=函数(){ 变量$sport=$(“#预约#客户”); $sport.change(函数(){ var$form=$(this).closest('form'); 变量数据={}; 数据[$sport.attr('name')]=$sport.val(); 数据['appointment[_token]]=$('appointment_token').val(); $.ajax({ url:$form.attr('action'), 类型:$form.attr('method'), 数据:数据, 成功:函数(html){ $(“#预约"疾病”)。替换为( $(html).查找(“#约会"疾病”) ); } }); }); }; {%endblock%}
    您可以在
    编辑操作中创建一个
    新约会
    ,然后将其保留。您应该获取函数参数中的一个,处理请求并刷新,因为您的对象已经被持久化了

    因此,请删除这些行: 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' )); } } 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(), )); } {% 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 %} {% 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 %}
    $appointment = new Appointment();
    // ...
    $em->persist($appointment);