Answer the question
In order to leave comments, you need to log in
How to add validation on unique field in doctrine?
Essence
<?php
namespace OwrBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass="OwrBundle\Entity\Repository\UserRepository")
* @ORM\Table(name="`user`")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(
* fields={"username", "phone"},
* message="duplicate"
* )
*/
class User implements UserInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer", name="user_id")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", name="email", unique=true)
* @Assert\Email(
* message = "email",
* checkMX = true
* )
*/
protected $username;
/**
* @ORM\Column(type="string", length=13, unique=true)
* @Assert\Regex(
* pattern = "/^\+\d{12}$/",
* message = "correct"
* )
*/
protected $phone;
/**
...
**/
}
SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "uniq_8d93d649444f97dd"
DETAIL: Key (phone)=(+180863723139) already exists.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question