hassen zouari
hassen zouari

Reputation: 171

create an update password in symfony2 doesn't work?

I have created an update password form , But It doesn't work :

It show this error message : The User object must implement the UserInterface interface.

the code :

Controller :

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;
    }

}

and 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';
    }
}

Upvotes: 1

Views: 647

Answers (1)

Rodrigo Mendez
Rodrigo Mendez

Reputation: 51

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

"This validates that an input value is equal to the current authenticated user's password"

Has your app an authenticated user at the moment of execute this code, and the authenticated user is an instance of a class that implements UserInterface interface?

Upvotes: 2

Related Questions