Reputation:
Hello guys i am try to do a form to modify the image of a profile. All work well but when i put an image with a big size (more than i except in the data base) i have this error :
Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed
I don't know why the constraints is not working ... (it works when i create a profile but not when i modify it) thx for all that will try to answer
I put my code right there : (controller)
$loggedAs = $this->getUser();
$avatar_profile = $loggedAs->getAvatarPath();
$em = $this->getDoctrine()->getManager();
$form = $this->createForm(ProfileModificationType::class, $loggedAs);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/**
* @var UploadedFile $file
*/
$file = $form->get('avatarPath')->getData();
if ($file != NULL) {
$fileName = md5(uniqid()) . '.' . $file->guessExtension();
$file->move(
$this->getParameter('image_directory'), $fileName
);
$loggedAs->setAvatarPath($fileName);
} else {
$loggedAs->setAvatarPath($avatar_profile);
}
$loggedAs->setSalt(md5(uniqid()));
$loggedAs->setPassword($encoder->encodePassword($loggedAs, $loggedAs->getPassword()));
$em->flush();
$this->get('session')->getFlashBag()->add('success', "Votre compte a été modifié");
Form :
->add('avatarPath', FileType::class, array(
'data_class' => null,
'required' => false,
'label' => 'Avatar',
'constraints' => array(
new Assert\Image(array(
'maxHeight' => 600,
'maxWidth' => 600,
'maxSize' => 1000000,
'maxHeightMessage' => 'Longeur maximale de 600Px',
'maxWidthMessage' => 'Largeur maximale de 600Px',
'maxSizeMessage' => 'Taille maximale de 1Mo',
))),
'invalid_message' => 'Cette valeur est invalide',
));
and in my entity profile (constraint):
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('avatarPath', new Assert\Image(array(
'maxHeight' => 600,
'maxWidth' => 600,
'maxSize' => 1000000,
'maxHeightMessage' => 'Longeur maximale de 600Px',
'maxWidthMessage' => 'Largeur maximale de 600Px',
'maxSizeMessage' => 'Taille maximale de 1Mo',
)));
}
and my entity
/**
* @var string
*
* @ORM\Column(name="avatar_path", type="string", length=255, nullable=false)
*/
private $avatarPath;
Upvotes: 0
Views: 1106
Reputation: 4582
It seems like you have not implemented the \Serializable
interface in your User
class.
Quoting from the symfony documentation:
At the end of each request, the User object is serialized to the session. On the next request, it's unserialized. To help PHP do this correctly, you need to implement Serializable.
Please refer to the documentation so that you better understand how to do this.
EDIT: The reason you are not getting this when creating a new User
object is because this user is not logged in so its object is not serialized. Only the logged-in User
object is serialized and stored to the session so that your application knows who is the currently logged-in user.
Even if you tried to edit a different user than the one that is logged-in you should not face that error for the same reasons.
Upvotes: 1