Php 使用Symfony2/Symfony3中的FOSUserBundle将用户名字段删除/替换为电子邮件
我只想有电子邮件作为登录方式,我不想有用户名。是否可以使用symfony2/symfony3和FOSUserbundle 我在这里读 但是,我遇到了两个违反约束的问题 问题是,如果用户将电子邮件地址留空,我会得到两个限制条件 违规行为:Php 使用Symfony2/Symfony3中的FOSUserBundle将用户名字段删除/替换为电子邮件,php,symfony,fosuserbundle,Php,Symfony,Fosuserbundle,我只想有电子邮件作为登录方式,我不想有用户名。是否可以使用symfony2/symfony3和FOSUserbundle 我在这里读 但是,我遇到了两个违反约束的问题 问题是,如果用户将电子邮件地址留空,我会得到两个限制条件 违规行为: 请输入用户名 请输入电子邮件 有没有一种方法可以禁用给定字段的验证,或者有更好的方法 是否从表单中删除一个字段 您是否尝试过自定义验证 为此,您需要从UserBundle继承自己的bundle,然后复制/调整Resources/config/validati
- 请输入用户名
- 请输入电子邮件
是否从表单中删除一个字段 您是否尝试过自定义验证
为此,您需要从UserBundle继承自己的bundle,然后复制/调整Resources/config/validation.xml。另外,您需要将config.yml中的验证组设置为自定义验证。正如Michael指出的,这可以通过自定义验证组解决。例如:
fos_user:
db_driver: orm
firewall_name: main
user_class: App\UserBundle\Entity\User
registration:
form:
type: app_user_registration
validation_groups: [AppRegistration]
然后在您的实体中(由user\u class:App\UserBundle\entity\user
定义),您可以使用AppRegistration组:
class User extends BaseUser {
/**
* Override $email so that we can apply custom validation.
*
* @Assert\NotBlank(groups={"AppRegistration"})
* @Assert\MaxLength(limit="255", message="Please abbreviate.", groups={"AppRegistration"})
* @Assert\Email(groups={"AppRegistration"})
*/
protected $email;
...
这就是我在将回复发布到Symfony2线程后所做的
有关详细信息,请参阅。我宁愿替换RegistrationFormHandler#进程,更精确地添加新方法processExtended(例如),它是原始方法的副本,并在RegistrationController中使用ut,而不是替换验证。(覆盖:) 在绑定注册表表单之前,我将用户名设置为“空”:
class RegistrationFormHandler extends BaseHandler
{
public function processExtended($confirmation = false)
{
$user = $this->userManager->createUser();
$user->setUsername('empty'); //That's it!!
$this->form->setData($user);
if ('POST' == $this->request->getMethod()) {
$this->form->bindRequest($this->request);
if ($this->form->isValid()) {
$user->setUsername($user->getEmail()); //set email as username!!!!!
$this->onSuccess($user, $confirmation);
/* some my own logic*/
$this->userManager->updateUser($user);
return true;
}
}
return false;
}
// replace other functions if you want
}
为什么??我更喜欢使用FOSUserBundle验证规则。因为如果我为注册表替换config.yml中的验证组,我需要在我自己的用户实体中为用户重复验证规则。需要执行的操作的完整概述
以下是需要完成的工作的完整概述。我在这篇文章的末尾列出了这里和那里的不同来源
1.覆盖Acme\UserBundle\Entity\User中的setter
2.从表单类型中删除用户名字段
(在RegistrationFormType
和ProfileFormType
中)
3.验证约束
如@nurikabe所示,我们必须摆脱FOSUserBundle
提供的验证约束,并创建自己的验证约束。这意味着我们必须重新创建以前在FOSUserBundle
中创建的所有约束,并删除与username
字段相关的约束。我们将创建的新验证组是acmerregistration
和AcmeProfile
。因此,我们完全覆盖了FOSUserBundle
提供的那些
3.a。更新Acme\UserBundle\Resources\config\config.yml中的配置文件
3.b。创建验证文件Acme\UserBundle\Resources\config\Validation.yml
这是很长的一段:
Acme\UserBundle\Entity\User:
properties:
# Your custom fields in your user entity, here is an example with FirstName
firstName:
- NotBlank:
message: acme_user.first_name.blank
groups: [ "AcmeProfile" ]
- Length:
min: 2
minMessage: acme_user.first_name.short
max: 255
maxMessage: acme_user.first_name.long
groups: [ "AcmeProfile" ]
# Note: We still want to validate the email
# See FOSUserBundle/Resources/config/validation/orm.xml to understand
# the UniqueEntity constraint that was originally applied to both
# username and email fields
#
# As you can see, we are only applying the UniqueEntity constraint to
# the email field and not the username field.
FOS\UserBundle\Model\User:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: email
errorPath: email
message: fos_user.email.already_used
groups: [ "AcmeRegistration", "AcmeProfile" ]
properties:
email:
- NotBlank:
message: fos_user.email.blank
groups: [ "AcmeRegistration", "AcmeProfile" ]
- Length:
min: 2
minMessage: fos_user.email.short
max: 255
maxMessage: fos_user.email.long
groups: [ "AcmeRegistration", "ResetPassword" ]
- Email:
message: fos_user.email.invalid
groups: [ "AcmeRegistration", "AcmeProfile" ]
plainPassword:
- NotBlank:
message: fos_user.password.blank
groups: [ "AcmeRegistration", "ResetPassword", "ChangePassword" ]
- Length:
min: 2
max: 4096
minMessage: fos_user.password.short
groups: [ "AcmeRegistration", "AcmeProfile", "ResetPassword", "ChangePassword"]
FOS\UserBundle\Model\Group:
properties:
name:
- NotBlank:
message: fos_user.group.blank
groups: [ "AcmeRegistration" ]
- Length:
min: 2
minMessage: fos_user.group.short
max: 255
maxMessage: fos_user.group.long
groups: [ "AcmeRegistration" ]
FOS\UserBundle\Propel\User:
properties:
email:
- NotBlank:
message: fos_user.email.blank
groups: [ "AcmeRegistration", "AcmeProfile" ]
- Length:
min: 2
minMessage: fos_user.email.short
max: 255
maxMessage: fos_user.email.long
groups: [ "AcmeRegistration", "ResetPassword" ]
- Email:
message: fos_user.email.invalid
groups: [ "AcmeRegistration", "AcmeProfile" ]
plainPassword:
- NotBlank:
message: fos_user.password.blank
groups: [ "AcmeRegistration", "ResetPassword", "ChangePassword" ]
- Length:
min: 2
max: 4096
minMessage: fos_user.password.short
groups: [ "AcmeRegistration", "AcmeProfile", "ResetPassword", "ChangePassword"]
FOS\UserBundle\Propel\Group:
properties:
name:
- NotBlank:
message: fos_user.group.blank
groups: [ "AcmeRegistration" ]
- Length:
min: 2
minMessage: fos_user.group.short
max: 255
maxMessage: fos_user.group.long
groups: [ "AcmeRegistration" ]
4.终点
就这样!你该走了
用于此职位的文件:
从Sf 2.3开始,快速解决方法是将用户名设置为扩展BaseUser的类用户的_构造中的任何字符串
public function __construct()
{
parent::__construct();
$this->username = 'username';
}
这样,验证程序就不会触发任何违规行为。但别忘了将电子邮件设置为发布者的用户名
您可能需要检查其他文件中对User:username的引用,并进行相应更改。我可以通过覆盖注册和配置文件表单类型详细信息并删除username字段来完成此操作
$builder->remove('username');
在我的具体用户类中重写setEmail方法的同时:
public function setEmail($email)
{
$email = is_null($email) ? '' : $email;
parent::setEmail($email);
$this->setUsername($email);
}
如果它们都不起作用,那么一个快速而肮脏的解决方案将是
public function setEmail($email)
{
$email = is_null($email) ? '' : $email;
parent::setEmail($email);
$this->setUsername(uniqid()); // We do not care about the username
return $this;
}
您可以使用户名为空,然后将其从表单类型中删除:
首先在AppBundle\Entity\User中,将注释添加到用户类上方
使用条令\ORM\Mapping\AttributeOverrides;
使用条令\ORM\Mapping\AttributeOverride;
/**
*使用者
*
*@ORM\Table(name=“fos\u user”)
*@AttributeOverrides({
*@AttributeOverride(name=“username”,
*column=@ORM\column(
*name=“username”,
*type=“string”,
*长度=255,
*唯一=错误,
*可为空=真
* )
* ),
*@AttributeOverride(name=“usernamecononical”,
*column=@ORM\column(
*name=“usernamecononical”,
*type=“string”,
*长度=255,
*唯一=错误,
*可为空=真
* )
* )
* })
*@ORM\Entity(repositoryClass=“AppBundle\Repository\UserRepository”)
*/
类用户扩展了BaseUser
{
//..
当您运行php-bin/console-doctor:schema:update--force时,它将使数据库中的用户名为空
Second,在表单类型AppBundle\form\RegistrationType中,从表单中删除用户名
public function buildForm(formbuilder接口$builder,数组$options)
{
$builder->remove('username');
//您可以使用->添加('field\u name')添加其他字段
}
现在,您将不会在表单中看到用户名字段(感谢$builder->remove('username');
)。提交表单时,您将不会再收到验证错误“请输入用户名”,因为不再需要它(感谢注释)
来源:我尝试过这个方法,但遇到了一些问题。除非扩展FOSUserBundle\Models\User,否则无法在实体上重新声明映射。但是,如果这样做,则必须显式重新声明所有内容。您是否真的能做到这一点?如果是这样,我将非常感谢您对所做的详细解释!别忘了添加“use Symfony\Component\Validator\Constraints as Assert;”,并将电子邮件设置为用户名:“public function setEmail($email){parent::setEmail($email);$this->setUsername($email);}”非常好
public function setEmail($email)
{
$email = is_null($email) ? '' : $email;
parent::setEmail($email);
$this->setUsername($email);
}
$builder->remove('username');
public function setEmail($email)
{
$email = is_null($email) ? '' : $email;
parent::setEmail($email);
$this->setUsername($email);
}
public function setEmail($email)
{
$email = is_null($email) ? '' : $email;
parent::setEmail($email);
$this->setUsername(uniqid()); // We do not care about the username
return $this;
}