php - 表单的 View 数据应该是类的一个实例......但是是一个(n)字符串

标签 php symfony data-class

我目前收到以下错误:

"The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File."

SoundController - 上传功能

 /**
 * @Security("is_granted('IS_AUTHENTICATED_FULLY')")
 * @Route("/song/upload", name="upload_song")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function uploadSong(Request $request)
{
    $song = new Sound();

    $form = $this->createForm(SoundType::class, $song);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid())
    {
        $file = $song->getFile();
        $user = $this->getUser();

        $fileName = $this
            ->get('app.file_uploader')
            ->setDir($this->get('kernel')->getRootDir()."/../web".$this->getParameter('songs_directory'))
            ->upload($file);

        $song->setFile($fileName);

        $file = $song->getCoverFile();
        if ($file === null)
        {
            $song->setCoverFile($this->getParameter('default_cover'));
        }
        else
        {
            $fileName = $this
                ->get('app.file_uploader')
                ->setDir($this->get('kernel')->getRootDir()."/../web".$this->getParameter('covers_directory'))
                ->upload($file);

            $song->setCoverFile($fileName);
        }

        $song->setUploader($user);
        $song->setUploaderID($user->getId());
        $user->addSong($song);

        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($song);
        $entityManager->flush();

        return $this->redirectToRoute('song_view', [
            'id' => $song->getId()
        ]);
    }

    return $this->render('song/upload.html.twig', [
        'form' => $form->createView()
    ]);
}

声音类型 - 形式

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('file', FileType::class)
        ->add('coverFile', FileType::class, [
            'required' => false
        ])
        ->add('songName', TextType::class)
        ->add('songAuthor', TextType::class);
}

最佳答案

这里是答案:

{
      $builder
          ->add('file', FileType::class, array('data_class' => null))
          ->add('coverFile', FileType::class, array('data_class' => null))
          ->add('coverFile', FileType::class, array('data_class' => null,'required' => false))
          ->add('songName', TextType::class)
          ->add('songAuthor', TextType::class);
  }

关于php - 表单的 View 数据应该是类的一个实例......但是是一个(n)字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40983353/

相关文章:

php - arsort使用什么算法?

java - 替换字符串中的多个单词 ln java like php str_replace

php - Doctrine2 从特征中复制属性和方法

PHP 不在 Mac OS X 上使用更新的 OpenSSL

android - 如何在 Kotlin Android 中为数据类创建构造函数?

python - 在 Python 中定义具有自动生成的 __init__ 和来自值字典的附加 init2 的数据类的正确方法是什么

php - 如果用户选择 `All`,如何从所有选项中检索数据

php - Laravel 数据库,我应该使用什么数据类型来存储护照号码?

css - 如何使用 Twig 使用基本CSS文件

kotlin - kotlin 中可以嵌套数据类吗?