Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

php - Symfony Unique Entity error message not displaying

I've tried to implement the unique entity from Symfony and when I try to insert with same email, it doesn't show the error message, but it shows instead a PHP Error.

The error :

An exception occurred while executing 'INSERT INTO user (id, email, firstname, lastname, password, registered_at, is_verified, forgotten_password_token, forgotten_password_requested_at, farm_id, discr) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["8006dc22-226a-4fe1-b6f2-0baf0ac1767f", "[email protected]", "Laurent", "Sanson", "$argon2id$v=19$m=65536,t=4,p=1$iKtkpJZhi/SAAQDip2YTyQ$7n2+LRh8p+KQEN/RzECrFDsxiouNAMyKuB6cdhMBIgY", "2021-01-05 19:53:36", 0, null, null, "478c39c3-12b3-4f0b-a877-b2d9edfbd6c5", "producer"]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'user.UNIQ_8D93D649E7927C74'

The beginning of my UserClass :

<?php

declare(strict_types=1);

namespace AppEntity;

use AppRepositoryUserRepository;
use DateTimeImmutable;
use DoctrineORMMapping as ORM;
use Serializable;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use SymfonyComponentSecurityCoreUserEquatableInterface;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentUidUuid;

/**
 * Class User
 * @package AppEntity
 * @ORMEntity(repositoryClass="AppRepositoryUserRepository")
 * @ORMInheritanceType("SINGLE_TABLE")
 * @ORMDiscriminatorColumn(name="discr", type="string")
 * @ORMDiscriminatorMap({"producer"="AppEntityProducer", "customer"="AppEntityCustomer"})
 * @UniqueEntity(
 *     fields="email",
 *     errorPath="email",
 *     message="Cet e-mail est déjà associé à un compte",
 *     entityClass="AppEntityUser"
 * )
 */
abstract class User implements UserInterface, Serializable, EquatableInterface
{
    /**
     * @ORMId
     * @ORMColumn(type="uuid")
     */
    protected Uuid $id;

    /**
     * @ORMColumn(unique=true)
     * @AssertNotBlank
     * @AssertEmail
     */
    protected string $email = "";
}

I've tried many things but nothing seems to work

/// EDIT /// The registration depends on the role but it'll be an user for sure. There is an inheritence between the User and the Customer/Producer My RegistrationController :

/**
     * @Route("/register/{role}", name="app_register")
     * @param string $role
     * @param Request $request
     * @param UserPasswordEncoderInterface $passwordEncoder
     * @return Response
     */
    public function register(string $role, Request $request, UserPasswordEncoderInterface $passwordEncoder): Response
    {
        $user = Producer::ROLE === $role ? new Producer() : new Customer();
        $user->setId(Uuid::v4());
        $form = $this->createForm(RegistrationFormType::class, $user, [
            "validation_groups" => ["Default" => "password"]
        ])->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $user->setPassword(
                $passwordEncoder->encodePassword($user, $user->getPlainPassword())
            );
            $this->getDoctrine()->getManager()->persist($user);
            $this->getDoctrine()->getManager()->flush();
            $this->addFlash("success", "Votre inscription a été effectuée avec succès");

            return $this->redirectToRoute('index');
        }

        return $this->render('ui/security/register.html.twig', [
            'registrationForm' => $form->createView(),
        ]);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The error in validation groups, it should be array of group like:

['Default', 'password']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...