问题描述
我只想将电子邮件作为登录方式,我不想拥有用户名.symfony2/symfony3 和 FOSUserbundle 可以吗?
I only want to have email as mode of login, I don't want to have username. Is it possible with symfony2/symfony3 and FOSUserbundle?
我在这里阅读http://groups.google.com/group/symfony2/browse_thread/thread/92ac92eb18b423fe
但后来我遇到了两个违反约束的问题.
But then I am stuck with two constraint violations.
问题是如果用户将电子邮件地址留空,我会得到两个约束违规行为:
Problem is if the user leaves the email address blank, I get two constraintviolations:
- 请输入用户名
- 请输入电子邮件
有没有办法禁用给定字段的验证,或者有更好的方法完全从表单中删除一个字段?
Is there a way to disable validation for a given field, or a better way toremove a field from the form altogether?
推荐答案
需要做的事情的完整概述
以下是需要完成的工作的完整概述.我在这篇文章的末尾列出了在这里和那里找到的不同来源.
A complete overview of what needs to be done
Here is a complete overview of what needs to be done. I have listed the different sources found here and there at the end of this post.
public function setEmail($email)
{
$email = is_null($email) ? '' : $email;
parent::setEmail($email);
$this->setUsername($email);
return $this;
}
2.从表单类型中删除用户名字段
(在 RegistrationFormType
和 ProfileFormType
中)
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
字段相关的约束.我们将创建的新验证组是 AcmeRegistration
和 AcmeProfile
.因此,我们完全覆盖了 FOSUserBundle
提供的那些.
3. Validation constraints
As shown by @nurikabe, we have to get rid of the validation constraints provided by FOSUserBundle
and create our own. This means that we will have to recreate all the constraints that were previously created in FOSUserBundle
and remove the ones that concern the username
field. The new validation groups that we will be creating are AcmeRegistration
and AcmeProfile
. We are therefore completely overriding the ones provided by the FOSUserBundle
.
fos_user:
db_driver: orm
firewall_name: main
user_class: AcmeUserBundleEntityUser
registration:
form:
type: acme_user_registration
validation_groups: [AcmeRegistration]
profile:
form:
type: acme_user_profile
validation_groups: [AcmeProfile]
3.b.创建验证文件 AcmeUserBundleResourcesconfigvalidation.yml
那是很长的一段:
3.b. Create Validation file AcmeUserBundleResourcesconfigvalidation.yml
That's the long bit:
AcmeUserBundleEntityUser:
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.
FOSUserBundleModelUser:
constraints:
- SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity:
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"]
FOSUserBundleModelGroup:
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" ]
FOSUserBundlePropelUser:
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"]
FOSUserBundlePropelGroup:
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.结束
就是这样!你应该没事了!
4. End
That's it! You should be good to go!
本文使用的文件:
- 从 FOSUserBundle 中删除用户名的最佳方法
- [验证] 未正确覆盖
- UniqueEntity
- 验证fosuserbundle注册表
- 如何使用验证组在 Symfony 中
- Symfony2 在表单中使用验证组
这篇关于使用 Symfony2/Symfony3 中的 FOSUserBundle 用电子邮件删除/替换用户名字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!