2016-04-21 67 views
0

我想用FOS實現密碼更改功能,但我不知道該怎麼做。實現更改密碼功能symfony 2與FOS

我開始用兩個屬性(舊密碼和新密碼(重複)創建一個表單

class ChangePasswordType extends UtilisateurType{ 

/** 
* @SecurityAssert\UserPassword(
*  message = "pswd ko" 
*) 
*/ 
protected $oldPassword; 

/** 
* @Assert\Length(
*  min = 7, 
*  max = 255, 
*  minMessage = "pswd too short" 
*  maxMessage = "pswd too long" 
*) 
*/ 
protected $newPassword; 

protected $user; 


public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('old', 'password', array('label' => 'Mot de passe actuel','attr' => array('placeholder' => 'Sasir votre mot de passe actuel','class' =>'form-control'))); 
    $builder->add('newPassword', 'repeated', array(
      'type' => 'password', 
      'invalid_message' => ' Les deux mots de passe ne sont pas identiques .', 
      'required' => true, 
      'first_options' => array('label' => 'Nouveau mot de passe', 'attr' => array('placeholder' => 'Sasir un nouveau mot de passe','class' =>'form-control')), 
      'second_options' => array('label' => 'Vérification', 'attr' => array('placeholder' => 'Vérification','class' =>'form-control')), 
    )); 
} 


/** 
* @return string 
*/ 
public function getName() 
{ 
    return 'Mybundle_changePassword'; 
} 

在我的控制,我創建的形式是這樣的:

 $form = $this->createForm(new ChangePasswordType(), null, array(
      'action' => $this->generateUrl('fos_user_change_password'), 
      'method' => 'POST', 
    )); 

在我小枝,我有這個表格和一個提交按鈕(動作:changePasswordActionChangePasswordController在FOS),但它不起作用。

      {{ form_start(formChangerPswd) }} 
           {{ form_widget(formChangerPswd) }} 
            <div class="box-footer"> 
             <input type="submit" value="Modify pswd" class="btn btn-primary pull-right" formnovalidate="formnovalidate"/> 
            </div><!-- /.box-footer --> 
           {{ form_end(formChangerPswd) }} 

我有這個錯誤

不能從類型「MyBundle \實體\用戶」的對象讀取索引「舊密碼」,因爲它沒有實現\ ArrayAccess接口。

這是實現此功能的最佳方式嗎? 有人可以幫我嗎?

+1

你究竟想要達到什麼目的?您是否注意到該包已經附帶了此功能的表單類型? – xabbuh

+0

我正在嘗試實現更改密碼功能。 – peace

+0

這並不能解釋爲什麼在重新使用FOSUserBundle已提供的功能的其餘部分時自己構建表單類型的原因。 – xabbuh

回答

0

如果您正在使用FOSUser捆綁你並不需要創建一個自定義密碼更改的邏輯,所有你需要做的就是去要實例化密碼更改樹枝模板,並添加這個

<a href="{{ path('fos_user_change_password') }}">Change Password</a> 

上述代碼是更改密碼過程的路由,它會調用fosUser changepassword控制器。

注意:如果您要新設置fosUser包,您需要覆蓋內置模板並將其自定義爲具有您網站的主題。如果你不知道怎麼做,如果你想從您的自定義控制器辦理更改密碼過程中,你可以訪問這個網站,它會給你一個很好的啓動
http://www.sitepoint.com/basic-user-management-in-symfony2-with-fosuserbundle/

namespace FOS\UserBundle\Controller; 

    use FOS\UserBundle\FOSUserEvents; 
    use FOS\UserBundle\Event\FormEvent; 
    use FOS\UserBundle\Event\FilterUserResponseEvent; 
    use FOS\UserBundle\Event\GetResponseUserEvent; 
    use FOS\UserBundle\Model\UserInterface; 
    use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
    use Symfony\Component\HttpFoundation\Request; 
    use Symfony\Component\HttpFoundation\RedirectResponse; 
    use Symfony\Component\Security\Core\Exception\AccessDeniedException; 

    /** 
    * your custom Controller managing the password change 
    * 
    * 
    */ 
    class ProfileController extends Controller 
    { 
    /** 
     * Change user password 
     */ 
     public function changePasswordAction(Request $request) 
     { 
    $user = $this->getUser(); 



    //dispatch the appropriate events 

    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */ 
    $dispatcher = $this->get('event_dispatcher'); 

    $event = new GetResponseUserEvent($user, $request); 
    $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_INITIALIZE,   $event); 

    if (null !== $event->getResponse()) { 
     return $event->getResponse(); 
    } 


/** 
    * this is where you start the initialization of the form to you use 
    */ 

    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface   */ 
    $formFactory = $this->get('fos_user.change_password.form.factory'); 

    $form = $formFactory->createForm(); 
     //pass in the user data 
    $form->setData($user); 

    $form->handleRequest($request); 

    if ($form->isValid()) { 
     /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */ 
     $userManager = $this->get('fos_user.user_manager'); 

     $event = new FormEvent($form, $request); 
     $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event); 

     $userManager->updateUser($user); 

     if (null === $response = $event->getResponse()) { 
      //here you set the url to go to after changing the password 
       //for example i am redirecting back to the page that triggered the change password process 
      $url = $this->generateUrl('showProfileAccount'); 
      $response = new RedirectResponse($url); 
     } 

     $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new FilterUserResponseEvent($user, $request, $response)); 

     return $response; 
    } 

    return $this->render('FOSUserBundle:ChangePassword:changePassword.html.twig', array(
     //note remove this comment. pass the form to template 
     'form' => $form->createView() 
    )); 
} 
} 

模板

{{ form_start(form) }} 
    {{ form_widget(form) }} 
    <div> 
    <input type="submit" value="{{ 'change_password.submit'|trans }}" /> 
    </div> 
    {{ form_end(form) }} 
+0

謝謝你的回答,但它不回答我的問題。我有一個用戶配置文件視圖,我想在這個視圖中集成更改密碼錶單。那麼,我必須在呈現profil視圖的控制器中初始化表單? – peace

+0

@peace等待讓我編輯我的答案,告訴你如何從你的自定義控制器初始化更改密碼過程 –

+0

Thnk的@Akoh,你修改它還是不是? – peace

0

如果您正在使用FOSUser捆綁你並不需要創建一個自定義密碼更改的邏輯,所有你需要做的就是去要實例化密碼更改樹枝模板,並添加這個

<a href="{{ path('fos_user_change_password') }}">Change Password</a> 

上述代碼是更改密碼過程的路由,它會調用fosUser changepassword控制器。

注意:如果您要新設置fosUser包,您需要覆蓋內置模板並將其自定義爲具有您網站的主題。如果你不知道該怎麼做,你可以訪問這個網站,它會給你一個很好的啓動
http://www.sitepoint.com/basic-user-management-in-symfony2-with-fosuserbundle/

但是如果你必須自己處理過程以下步驟是如何去了解它

如果你想從您的自定義控制器辦理變更密碼的過程

namespace FOS\UserBundle\Controller; 

    use FOS\UserBundle\FOSUserEvents; 
    use FOS\UserBundle\Event\FormEvent; 
    use FOS\UserBundle\Event\FilterUserResponseEvent; 
    use FOS\UserBundle\Event\GetResponseUserEvent; 
    use FOS\UserBundle\Model\UserInterface; 
    use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
    use Symfony\Component\HttpFoundation\Request; 
    use Symfony\Component\HttpFoundation\RedirectResponse; 
    use Symfony\Component\Security\Core\Exception\AccessDeniedException; 

    /** 
    * your custom Controller managing the password change 
    * 
    * 
    */ 
    class ProfileController extends Controller 
    { 
    /** 
     * Change user password 
     */ 
     public function changePasswordAction(Request $request) 
     { 
    $user = $this->getUser(); 



    //dispatch the appropriate events 

    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */ 
    $dispatcher = $this->get('event_dispatcher'); 

    $event = new GetResponseUserEvent($user, $request); 
    $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_INITIALIZE,   $event); 

    if (null !== $event->getResponse()) { 
     return $event->getResponse(); 
    } 


/** 
    * this is where you start the initialization of the form to you use 
    */ 

    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface   */ 
    $formFactory = $this->get('fos_user.change_password.form.factory'); 

    $form = $formFactory->createForm(); 
     //pass in the user data 
    $form->setData($user); 

    $form->handleRequest($request); 

    if ($form->isValid()) { 
     /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */ 
     $userManager = $this->get('fos_user.user_manager'); 

     $event = new FormEvent($form, $request); 
     $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event); 

     $userManager->updateUser($user); 

     if (null === $response = $event->getResponse()) { 
      //here you set the url to go to after changing the password 
       //for example i am redirecting back to the page that triggered the change password process 
      $url = $this->generateUrl('showProfileAccount'); 
      $response = new RedirectResponse($url); 
     } 

     $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new FilterUserResponseEvent($user, $request, $response)); 

     return $response; 
    } 

    return $this->render('FOSUserBundle:ChangePassword:changePassword.html.twig', array(
     //note remove this comment. pass the form to template 
     'form' => $form->createView() 
    )); 
} 
} 

模板

{{ form_start(form) }} 
    {{ form_widget(form) }} 
    <div> 
    <input type="submit" value="{{ 'change_password.submit'|trans }}" /> 
    </div> 
    {{ form_end(form) }}