在symfony2中创建更新密码?

时间:2015-03-10 08:26:44

标签: php forms symfony doctrine-orm

我创建了一个更新密码表单,每当我尝试提交数据时,都会收到The class 'RetailMapping\Bundle\UserBundle\Form\Model\ChangePassword' was not found in the chain configured namespaces AppBundle\Entity, RetailMapping\Bundle\UserBundle\Entity, RetailMapping\Bundle\CatalogBundle\Entity, RetailMapping\Bundle\LocationBundle\Entity, RetailMapping\Bundle\ClassificationBundle\Entity, RetailMapping\Bundle\RetailOutletBundle\Entity, FOS\UserBundle\Model这样的错误。我在这里做错了什么?当我尝试将数据保存在数据库中时发生错误。 这是我的代码。 这是我的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';
    }
}

这是我的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;
    }
}

这是我的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(),
        ]);
    }
}

2 个答案:

答案 0 :(得分:1)

在您关联的帖子中没有提及实体。因为你使这个类成为一个实体,它就变成了一个数据库表,并且期望像id这样的字段。

仔细检查您尝试实施的答案。

答案 1 :(得分:0)

要制作密码更新表单,您需要制作PasswordUpdateModelPasswordUpdateType,处理完表单后,您可以将其分配到UserEntity字段。

我希望这会有所帮助。