Symfony2: 如何使FOSUserBundle用户的用户名独一无二[英] Symfony2: How to make username of FOSUserBundle user unique

本文是小编为大家收集整理的关于Symfony2: 如何使FOSUserBundle用户的用户名独一无二的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有自己的User类,它继承了FOS\UserBundle\Entity\User.此外,我写了自己的注册例程.现在,我有一个问题,即该表单无法确保用户名是唯一的.我总是得到:

sqlState [23000]:违规的完整性约束:1062重复输入'myusername'for键'uniq_2da1797792fc23a8'

我尝试添加文档中所述的@UniqueEntity("email")注释,推荐答案

约束已经存在于FOS捆绑包中.您可能需要将表单上的validation_groups选项设置为array('Registration').

其他推荐答案

如果您使用的是fos_user捆绑包,则可以简单地使用唯一性约束: http://symfony.com/doc/2.0/reference/constraints/uniqueentity.html .

要实现它,只需确保您的用户类构成适当的使用语句,然后像这样的注释(假设您使用注释):

<?php
// ...
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 * @UniqueEntity("email")
 * @UniqueEntity("username")
*/
class User extends BaseUser
{ /* ... */ }

其他推荐答案

您可以在用户实体验证的情况下在validation.yml上尝试一下:

constraints:    
    - FOS\UserBundle\Validator\Unique:
        property: usernameCanonical
        message:  'This username already exists. Please choose another one.'

本文地址:https://www.itbaoku.cn/post/2090762.html

问题描述

I have my own User Class, which inherits FOS\UserBundle\Entity\User. Additionally I wrote my own registration routine. Now I have the problem that the form does not make sure that the username is unique. I always get:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'myusername' for key 'UNIQ_2DA1797792FC23A8'

I tried adding the @UniqueEntity("email") annotation as stated in the documentation1, but without any effect.

Someone knows what might be wrong?

推荐答案

The constraint exists in the FOS bundle already. You probably need to set the validation_groups option on your form to array('Registration').

其他推荐答案

If you're using the fos_user bundle, you can simply use the UniqueEntity constraint: http://symfony.com/doc/2.0/reference/constraints/UniqueEntity.html.

To implement it, just make sure your User class constains the proper use statements, and then the annotations, like so (assuming you're using annotations):

<?php
// ...
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 * @UniqueEntity("email")
 * @UniqueEntity("username")
*/
class User extends BaseUser
{ /* ... */ }

其他推荐答案

You can try this on the validation.yml with your user entity validation:

constraints:    
    - FOS\UserBundle\Validator\Unique:
        property: usernameCanonical
        message:  'This username already exists. Please choose another one.'