首页 > 解决方案 > Symfony 字段表单在 Ajax 之后未提交

问题描述

我在我的 Symfony 项目中制作了一个表单,其中一个选择字段的填充取决于另一个字段。我遵循了 Symfony 教程。

“位置”字段取决于“法院”字段。

https://symfony.com/doc/current/form/dynamic_form_modification.html

问题是在 ajax 调用之后填充的字段没有提交。

提交后我得到什么(位置似乎不存在)

    array:11 [▼
  "Tags" => "test"
  "Decided_at" => DateTime @1614297600 {#1254 ▶}
  "Resume" => "test"
  "Language" => App\Entity\Languages {#1436 ▶}
  "Regulation" => array:1 [▶]
  "SubRegulation" => Doctrine\Common\Collections\ArrayCollection {#1412 ▶}
  "Court" => App\Entity\Courts {#1478 ▶}
  "Domain" => Doctrine\Common\Collections\ArrayCollection {#1368 ▶}
  "Problematic" => Doctrine\Common\Collections\ArrayCollection {#1522 ▶}
  "originalFilename" => "doc008"
  "fullPathFile" => "C:\wamp64\www\igodb/public/uploads/doc008.pdf"
]

我的表单控制器

    <?php

class FileController extends AbstractController

{

    public function new(EntityManagerInterface $em, Request $request, SessionInterface $session) {

        $form = $this->createForm(FileFormType::class);
        $form->handleRequest($request);
        
        if ($form->isSubmitted() && $form->isValid() || $newTagsAllowed) {

            
            $doc->location = $data['Location']->getName();

            // rest

            return $this->redirectToRoute('search');
        }

        return $this->render('pages/upload.html.twig', [

            'fileForm' => $form->createView(),
            'title' => 'Upload a file'

        ]);
    }
}

我的表单类型

class FileFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options,...)
    {

        $builder    
            ->add('Location', EntityType::class, [
                'required' => true,
                'label_attr' => [ ' class' => 'font-weight-bold'],
                'attr' => [
                    'class' => 'form-control'
                ],
                'placeholder' => 'Please choose a location',
                'class' => Locations::class,
                'mapped' => true,
            ])

            ->add('Court', EntityType::class, [
                'required' => true,
                'data' => $courts,
                'label_attr' => [ ' class' => 'font-weight-bold'],
                 'attr' => [
                     'class' => 'form-control'
                 ],
                 'choice_attr' => [
                   'class' => 'form-check-input'
                 ],
                 'placeholder' => 'Please choose a court',
                 'class' => Courts::class
             ])

        $addLocations = function(FormInterface  $form, Courts $courts = null ){
            $arrayCourts = null === $courts ? [] : $courts->getLocations();

            $form->add('Location', EntityType::class, [
                'required' => true,
                'label_attr' => [ ' class' => 'font-weight-bold'],
                'attr' => [
                    'class' => 'form-control'
                ],
                'placeholder' => 'Please choose a Location',
                'class' => Locations::class,
                'mapped' => true,
                'choices' => $arrayCourts
            ]);
        };

        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function(FormEvent $event) use ($addLocations){
                $data = $event->getData();

                if($data === null){
                    return;
                }

                $addLocations($event->getForm(), $data->getLocations());
            }
        );

        $builder->get('Court')->addEventListener(
            FormEvents::POST_SUBMIT,
            function(FormEvent $event) use ($addLocations){
                $courts = $event->getForm()->getData();

                $addLocations($event->getForm()->getParent(), $courts);
            }
        );

    }
}

树枝文件中的 Ajax

var $courts = $('#file_form_Court')
            $courts.change(function(){
            var $form = $(this).closest('form');
            var data = {};
            data[$courts.attr('name')] = $courts.val();
            $.ajax({
                url: $form.attr('action'),
                type: $form.attr('method'),
                data: data,
                success: function(html){
                    $('#file_form_Location').replaceWith(
                    $(html).find('#file_form_Location')
                    );
                }
            });
         });

编辑:经过一番研究,我看到 AJAX 请求正在解除我的字段与表单的绑定。也许我需要重新绑定它,但是如何?

在 AJAX 之前

form: <form name="file_form" method="post" enctype="multipart/form-data">

AJAX 之后

form: null

标签: phpsymfony

解决方案


问题解决了。我重做我的实体并且它有效。不知道为什么...


推荐阅读