A complete overview of what needs to be done
这里全面概述了需要做的工作。 我列出了本职务结束时发现的不同来源。
1. Override setter in AcmeUserBundleEntityUser
public function setEmail($email)
{
$email = is_null($email) ? : $email;
parent::setEmail($email);
$this->setUsername($email);
return $this;
}
2. Remove the username field from your form type
(在和ProfileFormType
上)
public function buildForm(FormBuilder $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->remove( username ); // we use email as the username
//..
}
3. Validation constraints
如@nurikabe所示,我们必须消除FOSUserBundle提供的验证制约因素,并创建我们自己。 这意味着,我们将必须重新列出此前在<条码>上设置的所有制约因素,并删除<条码>用户名<>/代码>领域所涉及的制约因素。 我们将创建的新验证组为<代码>AcmeRegistration和AcmeProfile
。 因此,我们完全凌驾于SUserBundle所提供的代码之上。
3.a. Update config file in AcmeUserBundleResourcesconfigconfig.yml
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. Create Validation file AcmeUserBundleResourcesconfigvalidation.yml
长期以来:
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. End
!! 你们应当善待!
Documents used for this post: