php – 表单的视图数据应该是类的实例…但是是一个(n)字符串
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 表单的视图数据应该是类的实例…但是是一个(n)字符串,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2179字,纯文字阅读大概需要4分钟。
内容图文
![php – 表单的视图数据应该是类的实例…但是是一个(n)字符串](/upload/InfoBanner/zyjiaocheng/698/255fedc286de4b8783c6b4b8ce901d9c.jpg)
我目前收到以下错误:
“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()
]);
}
SoundType – 表格
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 – 表单的视图数据应该是类的实例…但是是一个(n)字符串全部内容,希望文章能够帮你解决php – 表单的视图数据应该是类的实例…但是是一个(n)字符串所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。