【问题标题】:symfony 3 file upload in edit formsymfony 3 以编辑形式上传文件
【发布时间】:2018-05-12 22:02:02
【问题描述】:

我有一个带有“图像”字段的实体“课程”。
这是 CoursType.php 的构建器

$builder
        ->add('titre')
        ->add('image', FileType::class, array('data_class' => null, 'label'=>false,'required'=>false))                                                                                       
        ->add('videoIntro')
            );

问题出在“编辑表单”中,当我提交此表单而不上传新图像时,空值将传递给“图像”。

这是editAction的控制器代码

public function editAction(Request $request, Cours $cour)
{   
    $em = $this->getDoctrine()->getManager();

    $deleteForm = $this->createDeleteForm($cour);


    $editForm = $this->createForm('LearnBundle\Form\CoursType', $cour);
    $editForm->handleRequest($request);

    if ($editForm->isSubmitted() && $editForm->isValid())
    {

        $image=$cour->getImage();
        $imageName = md5(uniqid()).'.'.$image->guessExtension();
        $image->move($this->getParameter('images_directory'),$imageName);
        $cour->setImage($imageName);

        $em->flush();

        return $this->redirectToRoute('cours_edit', array('id' => $cour->getId()));
    }

    return $this->render('cours/edit.html.twig', array(
        'cour' => $cour,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),

    ));
}

【问题讨论】:

  • 所以你需要在处理之前测试图像是否真的是由用户设置的,就像它是发送的一样
  • 请你给我推荐几行代码,因为我尝试了几十次,但我失败了

标签: php html file symfony


【解决方案1】:

应该是这样的:

public function editAction(Request $request, Cours $cour) {
    //fetch current img is exists
    $img=$cour->getImage();
    if($img !== null) {
        $cour->setImage(new File($this->getParameter('images_directory').$img);
    }

    $em = $this->getDoctrine()->getManager();
    $deleteForm = $this->createDeleteForm($cour);
    $editForm = $this->createForm('LearnBundle\Form\CoursType', $cour);
    $editForm->handleRequest($request);

    if ($editForm->isSubmitted() && $editForm->isValid()) {
        //Check if new image was uploaded
        if($cour->getImage() !== null) {
            //Type hint
            /** @var Symfony\Component\HttpFoundation\File\UploadedFile $newImage*/
            $newImage=$cour->getImage();
            $newImageName= md5(uniqid()).'.'.$file->guessExtension();
            $newImage->move($this->getParameter('images_directory'), $newImageName);
            $cour->setImage($newImageName);
        } else {
            //Restore old file name
            $cour->setImage($img);
        }

        $em->flush();

        return $this->redirectToRoute('cours_edit', array('id' => $cour->getId()));
    }

    return $this->render('cours/edit.html.twig', array(
        'cour' => $cour,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),

    ));
}

【讨论】:

  • 错误:在字符串“$imageName = md5(uniqid()).'.'.$image->guessExtension();”中调用成员函数guessExtension()跨度>
  • @FaroukDouglas 已编辑,忘记添加类型提示... ;)
  • 我的意思是我应该对这一行做哪些修改 "$imageName = md5(uniqid()).'.'.$image->guessExtension();"
  • 啊,抱歉,忘记删除您的代码了...再次更新... ;)
猜你喜欢
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 2018-04-12
  • 2022-01-05
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多