繁体   English   中英

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

[英]create an update password in symfony2 doesn't work?

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

它显示此错误消息: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';
    }
}

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

“这将验证输入值等于当前经过身份验证的用户的密码”

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM