I'm using Symfony 5 and in my User.php file there is a problem. Here is my code
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields= {"email","username"}, message="Username ou e-mail déjà utilisé")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Username
*/
private $username;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $familyName;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Email()
*/
private $email;
/**
* @ORM\Column(type="string", length=255)
* @Asser\Length(min=8, minMessage="Votre mot de passe doit faire minimum 8 caractères")
* @Assert\EqualTo(propertyPath="confirm_password")
*/
private $password;
/**
* @Assert\EqualTo(propertyPath="password", message="Vous n'avez pas tapé le même mot de passe")
*/
public $confirm_password;
public function getId(): ?int
{
return $this->id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getFamilyName(): ?string
{
return $this->familyName;
}
public function setFamilyName(string $familyName): self
{
$this->familyName = $familyName;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function eraseCredentials()
{
}
public function getSalt()
{
}
public function getRoles()
{
return ['ROLE_USER'];
}
public function getUserIdentifier()
{
} }
And it shows me this message :
[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\Username" in property App\Entity\User::$username was never imported. Did you maybe forget to add a "use" statement for this annotation?
But I already imported this
use Symfony\Component\Validator\Constraints as Assert;
So why am I getting this error ? Thank you
CodePudding user response:
Just use the Unique assertion with $username.
/**
* @ORM\Column(type="string", length=255)
* @Assert\Unique
*/
private $username;
Updated:
Having re-read your post, I don't think this answer is correct for your problem, depending on what you want.
If you want to insure that a username is not reused, and/or an email is not reused in the User table, then you need to use 2 separate uniqueentity annotations.
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields= {"username"}, message="Username déjà utilisé")
* @UniqueEntity(fields= {"email"}, message="E-mail déjà utilisé")
*/
As you have it now, you will only get a constraint error if someone uses the same email/username combination. It will be possible for multiple people to have the same username, and a person with the same email to use that to make multiple user accounts. I'm going to guess that is probably not what you want.