14

I am currently receiving the following error:

"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 - upload function

 /**
 * @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()
    ]);
}

SoundType - Form

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);
}
1

2 Answers 2

25

Here is the answer:

{
      $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);
  }
1
  • 1
    Can you elaborate on why this is the solution?
    – nbro
    Commented May 23 at 2:09
3
/**
 * @ORM\Column(type="string")
 *
 * @Assert\NotBlank(message="Please, upload the song as a MP3 file.")
 * @Assert\File(mimeTypes={ "audio/mpeg", "audio/wav", "audio/x-wav", "application/octet-stream" })
 */
private $file;

You tell doctrine that you want to store a string, but you render an upload button in the form which send you a physic file which you do not want to store in your database at all. Instead you want to move the file from a temporary directory to your upload directory, and you want to remember the name of the file into the database wherefore you need this property which is a string.

Best way is to follow this page

6
  • Please update your question with some code. Especial the action in your controller that processes the form
    – Frank B
    Commented Dec 6, 2016 at 11:58
  • what happens if you do a echo $filename; exit; after $this->get('app.file_uploader')...
    – Frank B
    Commented Dec 6, 2016 at 12:18
  • just before $song->setFile($fileName);
    – Frank B
    Commented Dec 6, 2016 at 13:02
  • Nothing happens. The same error. Besides if i put "exit;" the other statements are unreachable
    – P.Belberov
    Commented Dec 6, 2016 at 13:19
  • And what if you remove this annotation: @ORM\Column(type="string") above your $file property?
    – Frank B
    Commented Dec 6, 2016 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.