Reputation:
I've been getting errors when I press update.
I'm using the same code as adding.
I think it may be the picture upload that is causing the problem.
Here's the 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.
My Controller:
public function updateAction(Request $request ,$id)
{
$em = $this->getDoctrine()->getManager();
$forum = $em->getRepository("ForumBundle:Forum")->find($id);
$forum->setModifiee(new \DateTime('now'));
$form = $this->createForm(ForumType::class, $forum);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $form['image']->getData();
$file->move("images/", $file->getClientOriginalName());
$forum->setImage("images/" . $file->getClientOriginalName());
$em->persist($forum);
$em->flush();
return $this->redirectToRoute('forum_show');
}
return $this->render("@Forum/Sujet/Update_topic.html.twig", array("form" => $form->createView()));
}
My Form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('titre')
->add('tags')
->add('image', FileType::class,array('attr' => array(
'class'=>'form-control'
//'class'=>'btn btn-default btn-file'
)))
->add('blog',TextareaType::class)
->add('Ajouter',SubmitType::class,array('attr' => array(
'class'=>'theme_button color3 wide_button'
)));
}
These are my includes:
namespace ForumBundle\Controller;
use blackknight467\StarRatingBundle\Form\RatingType;
use ForumBundle\Form\RateType;
use ForumBundle\Entity\Commentaire;
use ForumBundle\Entity\Forum;
use ForumBundle\Entity\Rating;
use ForumBundle\Form\CommentaireType;
use ForumBundle\Form\ForumType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\File;
Upvotes: 0
Views: 553
Reputation: 609
In symfony 2.8 in updateAction just correct define fileType for $id I think it should look like this:
public function updateAction(Request $request ,$id) {
$id->setImage(
new File($this->getParameter('images_directory').'/'.$id->getImage()
));
...
Upvotes: 0
Reputation: 937
According to the Symfony documentation : How to upload file, you have to set a File Object into Image setter, not a string. That'a why you have this 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.
The rigth way to accomplish this is to set a new File instance to your image on your controller object, like this :
use Symfony\Component\HttpFoundation\File\File;
// ...
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$forum = $em->getRepository("ForumBundle:Forum")->find($id);
$forum->setModifiee(new \DateTime('now'));
$form = $this->createForm(ForumType::class, $forum);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$file = $form['image']->getData();
$file->move("images/", $file->getClientOriginalName());
$forum->setImage("images/" . $file->getClientOriginalName());
$forum->setImage(new File($this->getParameter('images_directory').'/'.$forum->getImage()));
$em->persist($forum);
$em->flush();
return $this->redirectToRoute('forum_show');
}
}
// ...
The parameter images_directory
have to be set in the services.yml file, according to the symfony's documentation :
# config/services.yaml
# ...
parameters:
# depending of your symfony version
images_directory: '%kernel.project_dir%/web|public/uploads/images'
Another way is to use an uploader service or a dedicated bundle like : VichUploaderBundle or OneupUploaderBundle
Hope this help and have a nice day !!!
Upvotes: 1