56

ログインのモードとして電子メールのみを使用したいのですが、ユーザー名は使用したくありません。symfony2/symfony3 と FOSUserbundle で可能ですか?

私はここで読んだhttp://groups.google.com/group/symfony2/browse_thread/thread/92ac92eb18b423fe

しかし、その後、2 つの制約違反で立ち往生しています。

問題は、ユーザーが電子メール アドレスを空白のままにすると、次の 2 つの制約違反が発生することです。

  • ユーザー名を入力してください
  • メールアドレスを入力してください

特定のフィールドの検証を無効にする方法、またはフォームからフィールドを完全に削除するより良い方法はありますか?

4

8 に答える 8

103

何をする必要があるかの完全な概要

ここでは、何を行う必要があるかについての完全な概要を示します。この投稿の最後に、あちこちで見つかったさまざまなソースをリストしました。

1.セッターをオーバーライドするAcme\UserBundle\Entity\User

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ます。作成する新しい検証グループは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.終了

それでおしまい!あなたは行く準備ができているはずです!


この記事で使用したドキュメント:

于 2014-01-11T16:15:31.633 に答える
2

Michael が指摘するように、これはカスタム検証グループで解決できます。例えば:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: App\UserBundle\Entity\User
    registration:
        form:
            type: app_user_registration
            validation_groups: [AppRegistration]

次に、エンティティ ( で定義user_class: App\UserBundle\Entity\User) で AppRegistration グループを使用できます。

class User extends BaseUser {

    /**
     * Override $email so that we can apply custom validation.
     * 
     * @Assert\NotBlank(groups={"AppRegistration"})
     * @Assert\MaxLength(limit="255", message="Please abbreviate.", groups={"AppRegistration"})
     * @Assert\Email(groups={"AppRegistration"})
     */
    protected $email;
    ...

これは、Symfony2 スレッドへの返信を投稿した後に行ったことです。

詳細については、 http://symfony.com/doc/2.0/book/validation.html#validation-groupsを参照してください。

于 2012-01-12T13:41:53.983 に答える
2

Sf 2.3 では、BaseUser を拡張するクラス User の _construct でユーザー名を任意の文字列に設定することで簡単に回避できます。

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

このようにして、バリデーターは違反をトリガーしません。ただし、電子メールをPattによって投稿されたユーザー名に設定することを忘れないでください。

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

User:username への参照について他のファイルを確認し、それに応じて変更する必要がある場合があります。

于 2014-10-02T18:55:08.493 に答える
1

それらのどれも機能しない場合、迅速で汚い解決策は

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

    return $this;
}
于 2016-05-29T17:04:06.627 に答える
1

Validation を置き換える代わりに、RegistrationFormHandler#process を置き換えることを好みます。より正確には、元のメソッドのコピーである新しいメソッド processExtended(たとえば) を追加し、RegistrationController で ut を使用します。(オーバーライド: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#next-steps )

登録フォームをバインドする前に、「空」などのユーザー名を設定します。

class RegistrationFormHandler extends BaseHandler
{

    public function processExtended($confirmation = false)
    {
        $user = $this->userManager->createUser();
        $user->setUsername('empty'); //That's it!!
        $this->form->setData($user);

        if ('POST' == $this->request->getMethod()) {


            $this->form->bindRequest($this->request);

            if ($this->form->isValid()) {

                $user->setUsername($user->getEmail()); //set email as username!!!!!
                $this->onSuccess($user, $confirmation);

                /* some my own logic*/

                $this->userManager->updateUser($user);
                return true;
            }
        }

        return false;
    }
    // replace other functions if you want
}

なんで?FOSUserBundle 検証ルールを使用することを好みます。登録フォームのconfig.ymlの検証グループを置き換えると、自分のユーザーエンティティでユーザーの検証ルールを繰り返す必要があります。

于 2012-09-24T11:39:14.993 に答える