This error occurs when I try to upload a bad file that does not match the image declaration. Only images are accepted.
custom object:
<?php
namespace Test\BackBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
class User implements UserInterface
{
private $id;
private $lastName;
private $firstName;
private $job;
private $email;
private $password;
private $salt;
private $roles;
private $isActive;
private $path;
public $file;
public function __construct()
{
$this->isActive = true;
$this->salt = md5(uniqid(null, true));
}
public function getId()
{
return $this->id;
}
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
public function getLastName()
{
return $this->lastName;
}
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
public function getFirstName()
{
return $this->firstName;
}
public function setJob($job)
{
$this->job = $job;
return $this;
}
public function getJob()
{
return $this->job;
}
public function setEmail($email)
{
$this->email = $email;
return $this;
}
public function getEmail()
{
return $this->email;
}
public function setPassword($password)
{
$this->password = $password;
return $this;
}
public function getPassword()
{
return $this->password;
}
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
public function getSalt()
{
return $this->salt;
}
public function setRoles($role)
{
if(array_diff($role, array("ROLE_SUPER_ADMIN", "ROLE_ADMIN", "ROLE_CUSTOMER"))) {
throw new \InvalidArgumentException("Bad role");
}
$this->roles = $role;
return $this;
}
public function getRoles()
{
return $this->roles;
}
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
public function getIsActive()
{
return $this->isActive;
}
public function eraseCredentials()
{
}
public function setUsername($email)
{
$this->email = $email;
return $this;
}
public function getUsername()
{
return $this->email;
}
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
return 'uploads/img';
}
public function upload()
{
if (null === $this->file) {
return;
} else {
$this->path = $this->firstName.'_'.$this->lastName.'_'.sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
}
$this->file->move($this->getUploadRootDir(), $this->path);
$this->file = null;
}
public function getPath()
{
return $this->getWebPath();
}
}
UserType:
$builder
->add('firstName', 'text', array(
'required' => true
))
->add('lastName', 'text', array(
'required' => true
))
->add('email', 'email', array(
'required' => true
))
->add('job', 'text', array(
'required' => false
))
->add('file', 'file', array(
'label' => false,
'required' => false,
))
;
:
public function updateMyAccountAction($id, Request $request)
{
$entityManager = $this->get('doctrine')->getManager();
$user = $this->get('doctrine')
->getRepository('TestBackBundle:User')
->find($id);
if (!$user) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$editForm = $this->createForm(new UserType(), $user);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$user->upload();
$entityManager->persist($user);
$entityManager->flush();
$this->get('session')->getFlashBag()->add('success', 'Your profile has been updated');
return $this->redirect($this->generateUrl('my_account', array('id' => $id)));
} else {
$this->get('session')->getFlashBag()->add('error', 'Erreur');
return $this->redirect($this->generateUrl('my_account', array('id' => $id)));
}
}
When I try to check if an image update, such as a pdf file, works, this error occurs. The file is not updated, so this is good. But my flash bag and redirection in my controller do not work ... if I write var_dump("test")in another in my controller "test", and the error also, so Symfony detects that the form is invalid.
This is part of the stack trace when an error occurs:
at / home / user / www / sf 2 / vendor / symfony / symfony / src / Symfony / Component / Security / Core / Authentication / Token / AbstractToken.php on line 155
152. $this->roles,
153. $this->attributes
154. )
155. );
}
/**
(array (object (User), true, array (object (Role)), array())) /home/kevin/www/sf 2/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php 155
, , role , ( , UserInterface)
, ?