The form's view data is expected to be an instance of class … but is a(n) string

前端 未结 2 455
旧巷少年郎
旧巷少年郎 2021-01-11 19:15

I am currently receiving the following error:

\"The form\'s view data is expected to be an instance of class Symfony\\Component\\HttpFoundation\\File

相关标签:
2条回答
  • 2021-01-11 19:46

    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);
      }
    
    0 讨论(0)
  • 2021-01-11 19:57
    /**
     * @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

    0 讨论(0)
提交回复
热议问题