Ran*_*ngh 57 php symfony fosuserbundle
我只想将电子邮件作为登录模式,我不想拥有用户名.是否可以使用symfony2/symfony3和FOSUserbundle?
我在这里阅读http://groups.google.com/group/symfony2/browse_thread/thread/92ac92eb18b423fe
但后来我遇到了两个违反约束的问题.
问题是如果用户将电子邮件地址留空,我会遇到两个约束违规:
有没有办法禁用给定字段的验证,或者更好的方法从表单中删除字段?
Mic*_*ick 104
以下是需要完成的工作的完整概述.我在本文末尾列出了这里和那里发现的不同来源.
Acme\UserBundle\Entity\Userpublic function setEmail($email)
{
$email = is_null($email) ? '' : $email;
parent::setEmail($email);
$this->setUsername($email);
return $this;
}
Run Code Online (Sandbox Code Playgroud)
(在这两个RegistrationFormType和ProfileFormType)
public function buildForm(FormBuilder $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->remove('username'); // we use email as the username
//..
}
Run Code Online (Sandbox Code Playgroud)
如@nurikabe所示,我们必须摆脱由FOSUserBundle我们自己提供的验证限制.这意味着我们将不得不重新创建之前创建的所有约束,FOSUserBundle并删除与该username字段有关的约束.我们将要创建的新验证组是AcmeRegistration和AcmeProfile.因此,我们完全凌驾于FOSUserBundle.
Acme\UserBundle\Resources\config\config.ymlfos_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]
Run Code Online (Sandbox Code Playgroud)
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" ]
Run Code Online (Sandbox Code Playgroud)
而已!你应该好好去!
用于此帖子的文件:
我能够通过覆盖此处详述的注册和配置文件表单类型以及删除用户名字段来完成此操作
$builder->remove('username');
Run Code Online (Sandbox Code Playgroud)
在我的具体用户类中覆盖setEmail方法:
public function setEmail($email)
{
$email = is_null($email) ? '' : $email;
parent::setEmail($email);
$this->setUsername($email);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24519 次 |
| 最近记录: |