首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用FOS实现修改密码函数symfony 2

用FOS实现修改密码函数symfony 2
EN

Stack Overflow用户
提问于 2016-04-21 08:07:19
回答 2查看 2.2K关注 0票数 0

我想用FOS实现密码更改功能,但我不知道怎么做。

首先,我创建了一个具有两个属性的表单(旧密码和新密码(重复)。

代码语言:javascript
复制
class ChangePasswordType extends UtilisateurType{ 

/**
 * @SecurityAssert\UserPassword(
 *     message = "pswd ko"
 * )
 */
protected $oldPassword;

/**
 * @Assert\Length(
 *     min = 7,
 *     max = 255,
 *     minMessage = "pswd too short"
 *     maxMessage = "pswd too long" 
 * )
 */
protected $newPassword;

protected $user;


public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('old', 'password', array('label' => 'Mot de passe actuel','attr' => array('placeholder' => 'Sasir votre mot de passe actuel','class' =>'form-control')));
    $builder->add('newPassword', 'repeated', array(
            'type' => 'password',
            'invalid_message' => ' Les deux mots de passe ne sont pas identiques  .',
            'required' => true,
            'first_options'  => array('label' => 'Nouveau mot de passe', 'attr' => array('placeholder' => 'Sasir un nouveau mot de passe','class' =>'form-control')),
            'second_options' => array('label' => 'Vérification', 'attr' => array('placeholder' => 'Vérification','class' =>'form-control')),
    ));
}


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

在我的控制器中,我创建如下形式:

代码语言:javascript
复制
        $form = $this->createForm(new ChangePasswordType(), null, array(
            'action' => $this->generateUrl('fos_user_change_password'),
            'method' => 'POST',
    ));

在我的小树枝上,我有一个表单和一个提交按钮(action:changePasswordAction of ChangePasswordController In FOS),但它不起作用。

代码语言:javascript
复制
                            {{ form_start(formChangerPswd) }}
                                {{ form_widget(formChangerPswd) }}
                                    <div class="box-footer">
                                        <input type="submit" value="Modify pswd" class="btn btn-primary pull-right" formnovalidate="formnovalidate"/>
                                    </div><!-- /.box-footer -->
                                {{ form_end(formChangerPswd) }}

我有一个错误

无法从"MyBundle\Entity\User“类型的对象中读取索引"oldPassword”,因为它没有实现\ArrayAccess。

这是实现此功能的最佳方式吗?有人能帮帮我吗?

EN

回答 2

Stack Overflow用户

发布于 2016-04-23 23:12:07

如果使用的是FOSUser包,则不需要创建自定义密码更改逻辑,只需转到要实例化密码更改并添加以下内容的小枝模板

代码语言:javascript
复制
<a href="{{ path('fos_user_change_password') }}">Change Password</a>

上面的代码是到更改密码进程的路由,它调用fosUser变更词控制器。

注意:如果您正在新设置fosUser包,则需要覆盖内置模板并自定义它以获得站点的主题。如果你不知道怎么做,你可以访问这个网站,它会给你一个良好的启动

http://www.sitepoint.com/basic-user-management-in-symfony2-with-fosuserbundle/

如果要处理来自自定义控制器的更改密码进程,则为。

代码语言:javascript
复制
    namespace FOS\UserBundle\Controller;

   use FOS\UserBundle\FOSUserEvents;
   use FOS\UserBundle\Event\FormEvent;
   use FOS\UserBundle\Event\FilterUserResponseEvent;
   use FOS\UserBundle\Event\GetResponseUserEvent;
   use FOS\UserBundle\Model\UserInterface;
   use Symfony\Bundle\FrameworkBundle\Controller\Controller;
   use Symfony\Component\HttpFoundation\Request;
   use Symfony\Component\HttpFoundation\RedirectResponse;
   use Symfony\Component\Security\Core\Exception\AccessDeniedException;

    /**
     * your custom Controller managing the password change
     *
     * 
     */
  class ProfileController extends Controller
     {
     /**
      * Change user password
       */
      public function changePasswordAction(Request $request)
       {
    $user = $this->getUser();



    //dispatch the appropriate events

    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
    $dispatcher = $this->get('event_dispatcher');

    $event = new GetResponseUserEvent($user, $request);
    $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_INITIALIZE,            $event);

    if (null !== $event->getResponse()) {
        return $event->getResponse();
    }


 /**
  * this is where you start the initialization of the form to you use
  */

    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface           */
    $formFactory = $this->get('fos_user.change_password.form.factory');

    $form = $formFactory->createForm();
      //pass in the user data
    $form->setData($user);

    $form->handleRequest($request);

    if ($form->isValid()) {
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');

        $event = new FormEvent($form, $request);
        $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event);

        $userManager->updateUser($user);

        if (null === $response = $event->getResponse()) {
            //here you set the url to go to after changing the password
              //for example i am redirecting back to the page  that triggered the change password process
            $url = $this->generateUrl('showProfileAccount');
            $response = new RedirectResponse($url);
        }

        $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

        return $response;
    }

    return $this->render('FOSUserBundle:ChangePassword:changePassword.html.twig', array(
       //note remove this comment. pass the form to template
        'form' => $form->createView()
    ));
}
}

模板

代码语言:javascript
复制
    {{ form_start(form) }}
     {{ form_widget(form) }}
     <div>
    <input type="submit" value="{{ 'change_password.submit'|trans }}" />
     </div>
  {{ form_end(form) }}
票数 0
EN

Stack Overflow用户

发布于 2016-04-27 18:53:31

如果使用的是FOSUser包,则不需要创建自定义密码更改逻辑,只需转到要实例化密码更改并添加以下内容的小枝模板

代码语言:javascript
复制
<a href="{{ path('fos_user_change_password') }}">Change Password</a>

上面的代码是到更改密码进程的路由,它调用fosUser变更词控制器。

注意:如果您正在新设置fosUser包,则需要覆盖内置模板并自定义它以获得站点的主题。如果你不知道怎么做,你可以访问这个网站,它会给你一个良好的启动

http://www.sitepoint.com/basic-user-management-in-symfony2-with-fosuserbundle/

,但是如果您必须自己处理这个过程,下面的过程是如何进行的,

如果要处理来自自定义控制器的更改密码进程,则为。

代码语言:javascript
复制
    namespace FOS\UserBundle\Controller;

   use FOS\UserBundle\FOSUserEvents;
   use FOS\UserBundle\Event\FormEvent;
   use FOS\UserBundle\Event\FilterUserResponseEvent;
   use FOS\UserBundle\Event\GetResponseUserEvent;
   use FOS\UserBundle\Model\UserInterface;
   use Symfony\Bundle\FrameworkBundle\Controller\Controller;
   use Symfony\Component\HttpFoundation\Request;
   use Symfony\Component\HttpFoundation\RedirectResponse;
   use Symfony\Component\Security\Core\Exception\AccessDeniedException;

    /**
     * your custom Controller managing the password change
     *
     * 
     */
  class ProfileController extends Controller
     {
     /**
      * Change user password
       */
      public function changePasswordAction(Request $request)
       {
    $user = $this->getUser();



    //dispatch the appropriate events

    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
    $dispatcher = $this->get('event_dispatcher');

    $event = new GetResponseUserEvent($user, $request);
    $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_INITIALIZE,            $event);

    if (null !== $event->getResponse()) {
        return $event->getResponse();
    }


 /**
  * this is where you start the initialization of the form to you use
  */

    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface           */
    $formFactory = $this->get('fos_user.change_password.form.factory');

    $form = $formFactory->createForm();
      //pass in the user data
    $form->setData($user);

    $form->handleRequest($request);

    if ($form->isValid()) {
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');

        $event = new FormEvent($form, $request);
        $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event);

        $userManager->updateUser($user);

        if (null === $response = $event->getResponse()) {
            //here you set the url to go to after changing the password
              //for example i am redirecting back to the page  that triggered the change password process
            $url = $this->generateUrl('showProfileAccount');
            $response = new RedirectResponse($url);
        }

        $dispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

        return $response;
    }

    return $this->render('FOSUserBundle:ChangePassword:changePassword.html.twig', array(
       //note remove this comment. pass the form to template
        'form' => $form->createView()
    ));
}
}

模板

代码语言:javascript
复制
    {{ form_start(form) }}
     {{ form_widget(form) }}
     <div>
    <input type="submit" value="{{ 'change_password.submit'|trans }}" />
     </div>
  {{ form_end(form) }}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36763504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档