2017-05-31 9 views
0

フォームを登録するときにパスワードを自動的に設定したかったのです。イベントをトリガーするにはREGISTRATION_INITIALIZEを使用します。残念ながら、それは動作していません。FOSUserBundleイベントREGISTRATION_INITIALIZEがトリガーされていません

リスナー:

<?php 

namespace Acme\UserBundle\EventListener; 

use Doctrine\ORM\EntityManager; 
use FOS\UserBundle\Event\UserEvent; 
use FOS\UserBundle\FOSUserEvents; 
use Symfony\Component\EventDispatcher\EventSubscriberInterface; 

class RegistrationListener implements EventSubscriberInterface 
{ 
    /** 
    * @var EntityManager 
    */ 
    private $em; 
/** 
* @param \Doctrine\ORM\EntityManager $entityManager 
*/ 
public function __construct(EntityManager $entityManager) 
{ 
    $this->em = $entityManager; 
} 

/** 
* {@inheritdoc} 
*/ 
public static function getSubscribedEvents() 
{ 
    return array(
     FOSUserEvents::REGISTRATION_INITIALIZE => 'onRegistrationInit', 
    ); 
} 

public function onRegistrationInit(UserEvent $userEvent) 
{ 
    $user = $userEvent->getUser(); 

    $user->setPassword('abcdeffffff'); 

} 

サービス:

#src/Acme/UserBundle/Resources/config/services.yml 
services: 
acme_user.registration: 
    class: Acme\UserBundle\EventListener\RegistrationListener 
    arguments: 
     entityManager: "@doctrine.orm.entity_manager" 
    tags: 
     - { name: kernel.event_subscriber} 

ので、パスワードが設定されていない、それはパスワードが空白であってはならない示しています。

私は間違っていますか?助けて!

編集:

問題は私が間違った場所でサービスを定義していたことでした。

src/Acme/UserBundle/Resources/config/services.ymlの代わりに、app/config/services.ymlである必要があります。

私はhttp://symfony.com/doc/master/bundles/FOSUserBundle/controller_events.htmlsrc/Acme/UserBundle/Resources/config/services.ymlを見て、しかし、私のために働いていませんでした!

+0

現在のsymfonyの3.3バージョンを使用していますか?いや、私はそれが理由ではないですが、3.2.8 – lordrhodos

+0

@lordrhodosを使用しています。私の編集を見てください – Arcv

答えて

0

たぶん私は間違っているが、私は、例えば、あなたのサービスの定義がfeatures from Symfony 3.3を使用していますが、Symfony 3.2.8を使用していると思います

バージョン3.3の新機能:名前($ adminEmail)で引数を設定する機能がSymfony 3.3に追加されました。以前は、あなただけのインデックス(この場合は2)、または他の引数に空の引用符を使用することによって、それを設定することができます。

にあなたのサービスの定義を更新してみてください。

#src/Acme/UserBundle/Resources/config/services.yml 
services: 
    acme_user.registration: 
     class: Acme\UserBundle\EventListener\RegistrationListener 
     arguments: ["@doctrine.orm.entity_manager"] 
     tags: 
      - { name: kernel.event_subscriber} 

と発言:あなたが実際にあなたがRegistrationSubscriberにクラスRegistrationListenerの名前を変更する必要があり、加入者を使用しているよう。

+0

ハズレを – Arcv

関連する問題