在 Symfony2 中创建更新密码



我创建了一个更新密码表单,每当我尝试提交数据时,我都会收到错误,例如The class 'RetailMappingBundleUserBundleFormModelChangePassword' was not found in the chain configured namespaces AppBundleEntity, RetailMappingBundleUserBundleEntity, RetailMappingBundleCatalogBundleEntity, RetailMappingBundleLocationBundleEntity, RetailMappingBundleClassificationBundleEntity, RetailMappingBundleRetailOutletBundleEntity, FOSUserBundleModel.我在这里做错了什么?当我尝试将数据保存在数据库中时发生错误。这是我的代码。这是我的更改密码类型.php

<?php
namespace RetailMappingBundleUserBundleForm;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
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' => 'RetailMappingBundleUserBundleFormModelChangePassword',
        ));
    }
    /**
     * @return string
     */
    public function getName()
    {
        return 'retailmapping_bundle_userbundle_password_chnage';
    }
}

这是我的更改密码.php

<?php
namespace RetailMappingBundleUserBundleFormModel;
use SymfonyComponentSecurityCoreValidatorConstraints as SecurityAssert;
use SymfonyComponentValidatorConstraints as Assert;
class ChangePassword
{
     /**
      * @SecurityAssertUserPassword(
      *     message = "Wrong value for your current password"
      * )
      */
     protected $oldPassword;
     /**
      * @AssertLength(
      *     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;
    }
}

这是我的用户控制器.php

<?php
namespace RetailMappingBundleUserBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use RetailMappingBundleUserBundleFormChangePasswordType;
use SymfonyComponentHttpFoundationRequest;
use RetailMappingBundleUserBundleFormModelChangePassword;
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(),
        ]);
    }
}

您链接的帖子中没有提到实体。由于您将类设置为实体,因此它已成为数据库表,并且需要像 id 这样的字段。

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

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

我希望这有所帮助。

相关内容

  • 没有找到相关文章

最新更新