2012-04-12 60 views
3

對於sf應用程序,我想從FOSUserBundle中的編輯配置文件表單中刪除密碼檢查。刪除編輯中的密碼檢查配置文件

只需通過覆蓋配置文件表單來刪除「當前」字段仍會導致「密碼無效」驗證消息。這是通過從FOSUSerBundle的ProfileFormHandler類引起的下列代碼:

$this->form->setData(new CheckPassword($user)); 

所以我overrided表單處理程序以及和替換

$this->form->setData($user); 

到目前爲止,這個作品和我的表單類型上面的代碼顯示和我的表單處理程序處理窗體,但我得到以下錯誤

The CSRF token is invalid. Please try to resubmit the form 

確實csrf令牌不再添加到窗體了。坦白說,我不知道我做錯了什麼;(

THX,本

這裏是形式的完整代碼,處理程序和模板:

<?php 

namespace Application\Sonata\UserBundle\Form\Type; 

use Symfony\Component\Form\FormBuilder; 

class ProfileFormType extends \FOS\UserBundle\Form\Type\ProfileFormType 
{ 

    private $class; 

    /** 
    * @param string $class The User class name 
    */ 
    public function __construct($class) 
    { 
     $this->class = $class; 
    } 

    public function buildForm(FormBuilder $builder, array $options) 
    { 
     $builder 
      ->add('first_name') 
      ->add('last_name') 
      ->add('phone') 
      ->add('location','room13_geo_location') 
      ->add('birthday','birthday') 
      ->add('smoker') 
      ->add('newsletter') 
     ; 


    } 

    public function getName() 
    { 
     return 'balkanride_user_profile'; 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => $this->class, 
      'intention' => 'profile', 
     ); 
    } 
} 

-

<?php 


namespace Application\Sonata\UserBundle\Form\Handler; 

use Symfony\Component\Form\Form; 
use Symfony\Component\HttpFoundation\Request; 

use FOS\UserBundle\Model\UserInterface; 
use FOS\UserBundle\Model\UserManagerInterface; 
use FOS\UserBundle\Form\Model\CheckPassword; 

class ProfileFormHandler 
{ 
    protected $request; 
    protected $userManager; 
    protected $form; 

    public function __construct(Form $form, Request $request, UserManagerInterface $userManager) 
    { 
     $this->form = $form; 
     $this->request = $request; 
     $this->userManager = $userManager; 
    } 

    public function process(UserInterface $user) 
    { 

     $this->form->setData($user); 

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

      //var_dump($this->form->getErrors()); 
      //die(); 

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

       $this->onSuccess($user); 

       return true; 
      } 

      // Reloads the user to reset its username. This is needed when the 
      // username or password have been changed to avoid issues with the 
      // security layer. 
      $this->userManager->reloadUser($user); 
     } 

     return false; 
    } 

    protected function onSuccess(UserInterface $user) 
    { 
     $this->userManager->updateUser($user); 
    } 
} 

-

{% extends "ApplicationSonataUserBundle::layout.html.twig" %} 


{% block page_body %} 

<section> 

    <form id="ProfileForm" action="{{ path('fos_user_profile_edit') }}" {{ form_enctype(form) }} method="POST" class="fos_user_profile_edit"> 
     {{ form_widget(form) }} 
     <div> 

      <div class="form-actions"> 
       <input type="submit" value="{{ 'profile.edit.submit'|trans }}" class="btn btn-primary" /> 
       <a href="{{path('fos_user_profile_show')}}" class="btn">{{ 'profile.edit.cancel'|trans }}</a> 
      </div> 

     </div> 



    </form> 

</section> 

{% endblock page_body %} 

回答

1

建議的解決此問題的方法是創建一個單獨的窗體來編輯在編輯之前不需要密碼保護的字段。最簡單,沒有黑客:)

+0

聽起來像一個很好的解決方案。這個問題非常古老,問題不再存在......我仍然會將你的答案標記爲完整性的正確答案;) – room13 2014-01-16 14:01:37