I'm trying to build a subscription form with Symfony4 and I tought it was working but it appears that when I try to upload a profile picture that is too big, I've got the following error : A circular reference has been detected when serializing the object of class "App\Entity\User" (configured limit: 1)
However I did set a constraint on my property profilePicture
regarding the file's maxSize the user will try to upload so I do not understand why this is happening (I've got all the other errors displaying well).
Here is the part of code regarding the property profilePicture :
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="Merci de bien vouloir sélectionner une image")
* @Assert\Image(
* minRatio="1",
* maxRatio="1",
* minWidth="250",
* minHeight="250",
* minRatioMessage="Votre photo de profil doit avoir un ratio de 1:1",
* maxRatioMessage="Votre photo de profil doit avoir un ratio de 1:1",
* minWidthMessage="Votre image doit faire minimum {{ minWidth }} de large",
* maxWidthMessage="Votre image doit faire minimun {{ minHeight }} de hauteur",
* maxSize="2M",
* maxSizeMessage="Votre image ne peut pas fait plus de 2M")
*/
private $profilePicture;
The HomeController dealing with the subscription form :
/**
* @Route("/", name="home")
*/
public function index(Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
{
//To Manage registration
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && !$form->isValid()) {
return $this->json([
"status" => "error(s)",
"errors" => $form->getErrors(true, true)
], 200);
}
if ($form->isSubmitted() && $form->isValid()) {
// move the file from the temp folder
$fileUploader = new FileUploader($this->getParameter('profile_pictures_directory'));
$profilePicture = $form['userProfile']['profilePicture']->getData();
if ($profilePicture) {
$profilePictureFilename = $fileUploader->upload($profilePicture);
$user->getUserProfile()->setProfilePicture($profilePictureFilename);
}
// encode the plain password
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form->get('plainPassword')->getData()
)
);
$user->setCreationDate(new \DateTime());
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
return $this->json(["status" => "success"]);
}
return $this->render('home/index.html.twig', [
'registrationForm' => $form->createView(),
]);
}
The FileUploader service :
<?php
namespace App\Service;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileUploader
{
private $targetDirectory;
public function __construct($targetDirectory)
{
$this->targetDirectory = $targetDirectory;
}
public function upload(UploadedFile $file)
{
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$fileName = $safeFilename.'-'.uniqid().'.'.$file->guessExtension();
try {
$file->move($this->getTargetDirectory(), $fileName);
} catch (FileException $e) {
}
return $fileName;
}
public function getTargetDirectory()
{
return $this->targetDirectory;
}
}
There is a OneToOne relation between the entity User and the entity UserProfile where complementary data regarding the User are stored.
I'd like this to simply display the error message regarding the file size like it does for all the other types of errors. Let me know if you need other parts of my code.