-1

I have created an update password form and whenever I try to submit the data I got error like The class XXX was not found in the chain configured namespaces. What am I doing wrong in here? The error occurs when I try to save the data in the database. I have tried the answer from implementing update password Here are my codes. This is my ChangePasswordType.php

<?php

namespace RetailMapping\Bundle\UserBundle\Form;

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

class ChangePasswordType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    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'),
        ));
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
                'data_class' => 'RetailMapping\Bundle\UserBundle\Form\Model\ChangePassword',
        ));
    }

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

This is my ChangePassword.php

<?php
namespace RetailMapping\Bundle\UserBundle\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;
    }
}

This is my UserController.php

<?php
namespace RetailMapping\Bundle\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use RetailMapping\Bundle\UserBundle\Form\ChangePasswordType;
use Symfony\Component\HttpFoundation\Request;
use RetailMapping\Bundle\UserBundle\Form\Model\ChangePassword;

class UserController extends Controller
{
public function editPasswordAction(Request $request)
    {
        $form = $this->createForm(new ChangePasswordType(), new ChangePassword());
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();
            $form->getData();
            $em = $this->getDoctrine()->getManager();
            $em->persist($data);
            $em->flush();
            dump($form->getData());
            die;
        }

        return $this->render('User/editPassword.html.twig', [
                'form' => $form->createView(),
        ]);
    }
}
Community
  • 1
  • 1
Raaz
  • 1,669
  • 2
  • 24
  • 48
  • The problem is solved. I added this code. `if ($form->isSubmitted() && $form->isValid()) { $data = $form->getData(); $user = $this->getUser(); $user->setPassword($data->getNewPassword()); $em = $this->getDoctrine()->getManager(); $em->persist($user); $em->flush(); }` – Raaz Mar 11 '15 at 10:44

1 Answers1

0

By default your entity is mapped to Entity namespace.

DoctrineExtension code on github

protected function getMappingObjectDefaultName()
{
    return 'Entity';
}

You need to place your ChangePassword.php within Entity folder.

UPDATE:

Similar question - you can look at

Community
  • 1
  • 1
  • Placed the changepassword.php file within entity folder but got error like **Class "RetailMapping\Bundle\UserBundle\Entity\ChangePassword"** is not a valid entity or mapped super class. do i add the **getMappingObjectDefaultName()** function in my code?? – Raaz Mar 11 '15 at 08:37
  • @RajShakya you are missing `/** * @ORM\Entity * @ORM\Table(name="YOUR_TABLE_NAME") */` at the top of your class – Peter Popelyshko Mar 11 '15 at 08:40
  • No, I have already checked the link and that is not relevant with my problem – Raaz Mar 11 '15 at 08:42
  • @RajShakya look at this answer - http://stackoverflow.com/questions/7820597/class-xxx-is-not-a-valid-entity-or-mapped-super-class-after-moving-the-class-i – Peter Popelyshko Mar 11 '15 at 08:44
  • @RajShakya run this command `php app/console doctrine:mapping:info` and show result please – Peter Popelyshko Mar 11 '15 at 08:58
  • I ran the command but it could not find my changepassword.php file. why? – Raaz Mar 11 '15 at 09:04