English 中文(简体)
移除/替换用户名称领域,采用电子邮件方式,将FOSUserBundle用于地名2
原标题:Remove / Replace the username field with email using FOSUserBundle in Symfony2 / Symfony3

我只想用电子邮件作为标识,我不想有用户名称。 是否有可能使用假冒/传真3和FOSUserbundle?

http://groups.google.com/group/symfony2/browse_thread/thread/92ac92eb18b423fe”rel=“noreferer”

但随后,我陷入了两次违反限制行为。

Problem is if the user leaves the email address blank, I get two constraint violations:

  • Please enter a username
  • Please enter an email

Is there a way to disable validation for a given field, or a better way to remove a field from the form altogether?

最佳回答

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:

问题回答

我之所以能够这样做,是因为超越了详细的登记和简介表格类型here,并删除了用户名称。

$builder->remove( username );

在我具体的用户类别中,除了采用固定电子邮件方法外:

 public function setEmail($email) 
 {
    $email = is_null($email) ?    : $email;
    parent::setEmail($email);
    $this->setUsername($email);
  }

正如迈克尔指出的,这可以由一个习俗验证小组解决。 例如:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: AppUserBundleEntityUser
    registration:
        form:
            type: app_user_registration
            validation_groups: [AppRegistration]

之后,在贵实体(根据<代码>用户类别: AppUserBundleEntityUser的定义)中,你可以使用申请组:

class User extends BaseUser {

    /**
     * Override $email so that we can apply custom validation.
     * 
     * @AssertNotBlank(groups={"AppRegistration"})
     * @AssertMaxLength(limit="255", message="Please abbreviate.", groups={"AppRegistration"})
     * @AssertEmail(groups={"AppRegistration"})
     */
    protected $email;
    ...

这是我在对书状2的复函之后结束的。

见http://symfony.com/doc/2.0/book/validation.html#validation-groups”rel=“nofollow”http://symfony.com/doc/2.0/book/validation.html#validation-groups。

如Sf2.3所示,quick workaround是指将用户名称设定到贵国哪类用户的施工中。

public function __construct()
    {
        parent::__construct();
        $this->username =  username ;
    }

这样,有效者就挑起任何违法行为。 但是,不要忘记把电子邮件交给用户名称,如

public function setEmail($email)
{
    $email = is_null($email) ?    : $email;
    parent::setEmail($email);
    $this->setUsername($email);
}

您可能必须检查其他文件,以便查阅用户:用户名称和相应的改动。

您是否尝试根据具体情况进行验证?

为此,您必须自封地继承用户Bundle,然后复制/调整资源/汇/打字。 此外,你需要确定鉴定小组,以证实你的习俗。

如果他们不工作,那么迅速和 d脏的解决办法就是

public function setEmail($email)
{
    $email = is_null($email) ?    : $email;
    parent::setEmail($email);
    $this->setUsername(uniqid()); // We do not care about the username

    return $this;
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...