成功返回Symfony的验证错误

问题描述 投票:0回答:1

我有一个带有@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,
        ]);
    }
symfony symfony-forms symfony-validator
1个回答
0
投票

[基本上,有两种可能性:

© www.soinside.com 2019 - 2024. All rights reserved.