gpt4 book ai didi

php - 使用 Symfony2/Symfony3 中的 FOSUserBundle 将用户名字段删除/替换为电子邮件

转载 作者:IT老高 更新时间:2023-10-28 12:06:14 25 4
gpt4 key购买 nike

我只想使用电子邮件作为登录方式,我不想拥有用户名。 symfony2/symfony3 和 FOSUserbundle 可以吗?

我在这里阅读 http://groups.google.com/group/symfony2/browse_thread/thread/92ac92eb18b423fe

但后来我遇到了两个违反约束的情况。

问题是如果用户将电子邮件地址留空,我会得到两个约束违规:

  • 请输入用户名
  • 请输入邮箱

有没有办法禁用给定字段的验证,或者有更好的方法完全从表单中删除一个字段?

最佳答案

需要做什么的完整概述

这里是需要做什么的完整概述。我在这篇文章的末尾列出了在这里和那里找到的不同来源。

1。覆盖 Acme\UserBundle\Entity\User

中的 setter
public function setEmail($email)
{
$email = is_null($email) ? '' : $email;
parent::setEmail($email);
$this->setUsername($email);

return $this;
}

2。从表单类型中删除用户名字段

(在 RegistrationFormTypeProfileFormType 中)

public function buildForm(FormBuilder $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->remove('username'); // we use email as the username
//..
}

3。验证约束

如@nurikabe 所示,我们必须摆脱FOSUserBundle 提供的验证约束并创建自己的。这意味着我们必须重新创建之前在 FOSUserBundle 中创建的所有约束,并删除与 username 字段相关的约束。我们将创建的新验证组是 AcmeRegistrationAcmeProfile。因此,我们完全覆盖了 FOSUserBundle 提供的那些。

3.a.更新 Acme\UserBundle\Resources\config\config.yml

中的配置文件
fos_user:
db_driver: orm
firewall_name: main
user_class: Acme\UserBundle\Entity\User
registration:
form:
type: acme_user_registration
validation_groups: [AcmeRegistration]
profile:
form:
type: acme_user_profile
validation_groups: [AcmeProfile]

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。结束

就是这样!你应该很高兴!


本文使用的文档:

关于php - 使用 Symfony2/Symfony3 中的 FOSUserBundle 将用户名字段删除/替换为电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8832916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com