在symfony2中创建更新密码不起作用?

时间:2015-12-05 14:43:46

标签: php symfony

我创建了一个更新密码表单,但它不起作用:

它显示以下错误消息: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 个答案:

答案 0 :(得分:2)

请参阅http://symfony.com/doc/current/reference/constraints/UserPassword.html

“这验证输入值等于当前已验证用户的密码”

您的应用程序在执行此代码时是否已经过身份验证,并且经过身份验证的用户是实现UserInterface接口的类的实例?