I have a Symfony entity with @UniqueEntity
annotation. I use it so I don't get double records in my database with the same email
but I don't want to return to the user that the email is already in the database (privacy reasons).
How can I use @UniqueEntity
but catch the error and return to the user a success message?
Controller code:
public function register(Request $request, EntityManagerInterface $entityManager, TranslatorInterface $translator): Response
{
$success = false;
$newsletter = new Newsletter();
$form = $this->createForm(NewsletterType::class, $newsletter);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$newsletter = $form->getData();
$entityManager->persist($newsletter);
$entityManager->flush();
$this->addFlash('success', $translator->trans('newsletter.signed_up'));
return $this->redirectToRoute('landing');
}
return $this->render('landing/landing.html.twig', [
'newsletter_form' => $form->createView(),
'newsletter_success' => $success,
]);
}