我有一个带有@UniqueEntity
注释的Symfony实体。我使用它是为了避免在数据库中使用相同的email
来获得重复记录,但是我不想返回给用户该电子邮件已经在数据库中(隐私原因)。
我如何使用@UniqueEntity
但发现错误并向用户返回成功消息?
控制器代码:
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,
]);
}
[基本上,有两种可能性: