I want to ensure no duplicate username’s when validating my entity
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=32)
* @Assert\NotBlank();
* @Assert\Callback(methods={"isUniqueUsername"})
*/
private $name;
I suppose I need to use the Validator Callback. Then I will have to query database for users with that username, so I require entity manager? How do I access it? And access it “correctly”? Do I use a validator class instead? It seems quite troublesome to create a class just to validate 1 field?
In symfony2, there is a Unique validator you can use to ensure that a username, email address or any other field is unique. When using annotations, it works like this:
When trying to add a user through a form, you should get the message stated in the Unique annotation.