Symfony字段表单在Ajax之后没有提交



我在Symfony项目中创建了一个表单,其中一个选择字段的填充依赖于另一个字段。我遵循Symfony教程。

字段"location";这取决于"法院"。场。

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

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

提交后得到的结果(Location似乎不存在)

array:11 [▼
"Tags" => "test"
"Decided_at" => DateTime @1614297600 {#1254 ▶}
"Resume" => "test"
"Language" => AppEntityLanguages {#1436 ▶}
"Regulation" => array:1 [▶]
"SubRegulation" => DoctrineCommonCollectionsArrayCollection {#1412 ▶}
"Court" => AppEntityCourts {#1478 ▶}
"Domain" => DoctrineCommonCollectionsArrayCollection {#1368 ▶}
"Problematic" => DoctrineCommonCollectionsArrayCollection {#1522 ▶}
"originalFilename" => "doc008"
"fullPathFile" => "C:wamp64wwwigodb/public/uploads/doc008.pdf"
]

我formController

<?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'
]);
}
}

MyFormType

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

问题解决。我重做了我的实体,它成功了。不知道为什么…

相关内容

  • 没有找到相关文章

最新更新