2013-08-05 53 views
1

我正在使用Symfony 2 + FOSUserBundle來管理我的用戶。 問題是我想從用戶名中手動創建電子郵件地址,因此,不需要任何電子郵件字段。用FOSUserBundle手動設置電子郵件

我會從用戶名創建電子郵件,因爲一個要求註冊是從我的大學,遵循嚴格的格式([email protected])的電子郵件。

我設法重寫RegistrationFormType以避免被添加到我的註冊頁的電子郵件字段,但我仍然有錯誤「請輸入一個電子郵件」時,我提交表單。

如何防止電子郵件地址的驗證以及如何從用戶名「建立」它?

謝謝!

(對不起,我的英語,我知道這不是完美的......)

回答

1

你應該寫事件偵聽器fos_user.registration.initialize。從代碼文檔:

/** 
* The REGISTRATION_INITIALIZE event occurs when the registration process is initialized. 
* 
* This event allows you to modify the default values of the user before binding the form. 
* The event listener method receives a FOS\UserBundle\Event\UserEvent instance. 
*/ 
const REGISTRATION_INITIALIZE = 'fos_user.registration.initialize'; 

更多有關事件調度​​信息:http://symfony.com/doc/current/components/event_dispatcher/introduction.html 而例如事件監聽器:http://symfony.com/doc/current/cookbook/service_container/event_listener.html

更新 - 如何編寫代碼?

在你config.yml(或services.yml或其他擴展像xmlphp)這樣定義服務:

demo_bundle.listener.user_registration: 
    class:  Acme\DemoBundle\EventListener\Registration 
    tags: 
     - { name: kernel.event_listener, event: fos_user.registration.initialize, method: overrideUserEmail } 

接下來,定義監聽器類:

namespace Acme\DemoBundle\EventListener; 

class Registration 
{ 
    protected function overrideUserEmail(UserEvent $args) 
    { 
     $request = $args->getRequest(); 
     $formFields = $request->get('fos_user_registration_form'); 
     // here you can define specific email, ex: 
     $email = $formFields['username'] . '@sth.com'; 
     $formFields['email'] = $email; 
     $request->request->set('fos_user_registration_form', $formFields); 
    } 
} 

注意:當然,你可以通過向聽衆注入@validator來驗證此電子郵件。現在

你應該隱藏在登記表email場。你可以做到這一點通過重寫register_content.html.twig或(在我oppinion更好的方式)覆蓋FOS RegistrationFormType這樣的:

namespace Acme\DemoBundle\Form\Type; 

use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType; 
use Symfony\Component\Form\FormBuilderInterface; 

class RegistrationFormType extends BaseType 
{ 
    // some code like __construct(), getName() etc. 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      // some code for your form builder 
      ->add('email', 'hidden', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle')) 
     ; 
    } 
} 

現在您的應用程序已經準備好手動設置電子郵件。

+0

只要他使用相同的表單,驗證將始終失敗 – ferdynator

+1

@ byf-ferdy我添加了「如何編碼?」。 – NHG

3

有它周圍的簡單方法。它甚至是mentioned in the official FOSUserBundle documentation。你只需要重寫控制器。

創建自己的包和擴展FOSUserBundle:

class CustomUserBundle extends Bundle 
{ 
    public function getParent() 
    { 
     return 'FOSUserBundle'; 
    } 
} 

然後覆蓋RegistrationController

class RegistrationController extends BaseController 
{ 
    public function registerAction(Request $request) 
    { 
     // here you can implement your own logic. something like this: 
     $user = new User(); 
     $form = $this->container->get('form.factory')->create(new RegistrationType(), $user); 
     if ($request->getMethod() == 'POST') { 
      $form->bind($request); 
      if ($form->isValid()) { 
       $user->setEmail($user->getUsername() . '@' . $user->getSchoolname() . '.fr'); 
       // and what not. Also don't forget to either activate the user or send an activation email 

      } 
     } 
    } 
} 
+0

那麼這是一個很好的解決方案,但是管理一個領域不是太過分了嗎? – user2654454

+0

還有其他的方法。但這是恕我直言,最乾淨的方式。如果您稍後想要修改此項或添加新字段/刪除一些此解決方案可以輕鬆擴展。 – ferdynator