2015-12-05 58 views
1

我創建了更新密碼錶單,但它不起作用:在symfony2中創建更新密碼不起作用?

它顯示此錯誤消息:User對象必須實現UserInterface接口。

的代碼:

控制器:

public function editpasswordlaunchAction(users $users, Request $request) { 

      //$form = $this->createForm(new usersTypeupdatepassword(), $users); 


    $changePasswordModel = new ChangePassword(); 
    $form = $this->createForm(new ChangePasswordType(), $changePasswordModel); 
    $form->handleRequest($request); 

    if ($form->isValid() && $form->isSubmitted()) { 
     $em = $this->getDoctrine()->getManager(); 
     $user = $form->getData(); 
     $em->persist($user); 
     $em->flush(); 

     $this->get('session')->getFlashBag()->add(
       'notice', array(
      'alert' => 'success', 
      'title' => 'Success!', 
      'message' => 'Updated' 
       ) 
     ); 
    } 
    return $this->render('socialBundle:profile:edit.html.twig', array(
       'passwordupdate' => 'TRUE', 
       'form' => $form->createView(), 
    )); 

Changepassword.php:

<?php 

namespace social\socialBundle\Form\Model; 

use Symfony\Component\Security\Core\Validator\Constraints as SecurityAssert; 
use Symfony\Component\Validator\Constraints as Assert; 

class ChangePassword { 

    /** 
    * @SecurityAssert\UserPassword(
    *  message = "Wrong value for your current password" 
    *) 
    */ 
    protected $oldPassword; 

    /** 
    * @Assert\Length(
    *  min = 6, 
    *  minMessage = "Password should by at least 6 chars long" 
    *) 
    */ 
    protected $newPassword; 

    public function setOldPassword($oldPassword) { 
     $this->oldPassword = $oldPassword; 
    } 

    public function getOldPassword() { 
     return $this->oldPassword; 
    } 

    public function setNewPassword($newPassword) { 
     $this->newPassword = $newPassword; 
    } 

    public function getNewPassword() { 
     return $this->newPassword; 
    } 

} 

和ChangeformType.php:

<?php 

namespace social\socialBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 

class ChangePasswordType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('oldPassword', 'password'); 
     $builder->add('newPassword', 'repeated', array(
      'type' => 'password', 
      'invalid_message' => 'The password fields must match.', 
      'required' => true, 
      'first_options' => array('label' => 'Password'), 
      'second_options' => array('label' => 'Repeat Password'), 
     )); 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'social\socialBundle\Form\Model\ChangePassword', 
     )); 
    } 

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

回答

1

看到http://symfony.com/doc/current/reference/constraints/UserPassword.html

「這驗證了一個輸入值等於當前已驗證用戶的密碼」

有您的應用程序在執行此代碼的那一刻身份驗證的用戶,並通過認證的用戶是一類,它實現的一個實例UserInterface接口?

+0

那麼,如何鏈接它們? –

+0

當然。你可以看到https://github.com/symfony/security-core/blob/3794744ce6073e748710a6360c0e4328f7ac78c1/Validator/Constraints/UserPasswordValidator.php和https://symfony.com/doc/current/book/security.html –