我想用FOS实现密码更改功能,但我不知道该怎么做。
我首先创建了一个包含两个属性的表单(旧密码和新密码(重复)。
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';
}
在我的控制器中,我创建了这样的表单:
$form = $this->createForm(new ChangePasswordType(), null, array(
'action' => $this->generateUrl('fos_user_change_password'),
'method' => 'POST',
));
在我的树枝中,我有这个表单和提交按钮(FOS中changePasswordAction
的{{1}}行动),但它不起作用。
ChangePasswordController
我有错误:
无法从“MyBundle \ Entity \ User”类型的对象读取索引“oldPassword”,因为它没有实现\ ArrayAccess。
这是实现此功能的最佳方式吗? 请问有人可以帮助我吗?
答案 0 :(得分:0)
如果您使用的是FOSUser套件,则不需要创建自定义密码更改逻辑,您只需转到要实例化密码更改的树枝模板并添加此
<a href="{{ path('fos_user_change_password') }}">Change Password</a>
上面的代码是更改密码进程的路由,它调用fosUser changepassword控制器。
注意:如果您要新设置fosUser软件包,则需要覆盖内置模板并将其自定义为具有您网站的主题。如果你不知道如何做到这一点,你可以访问这个网站,它会给你一个良好的创业公司 http://www.sitepoint.com/basic-user-management-in-symfony2-with-fosuserbundle/
但是
如果您要从自定义控制器处理更改密码过程
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()
));
}
}
模板
{{ form_start(form) }}
{{ form_widget(form) }}
<div>
<input type="submit" value="{{ 'change_password.submit'|trans }}" />
</div>
{{ form_end(form) }}
答案 1 :(得分:0)
如果您使用的是FOSUser软件包,则无需创建自定义密码更改逻辑,您只需转到要实例化密码更改的树枝模板并添加此
<a href="{{ path('fos_user_change_password') }}">Change Password</a>
上面的代码是更改密码进程的路由,它调用fosUser changepassword控制器。
注意:如果您要新设置fosUser软件包,则需要覆盖内置模板并将其自定义为具有您网站的主题。如果你不知道怎么做,你可以访问这个网站,它会给你一个很好的启动 http://www.sitepoint.com/basic-user-management-in-symfony2-with-fosuserbundle/
但如果你必须自己处理这个过程,下面的程序是如何实现的
如果您要从自定义控制器处理更改密码过程
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()
));
}
}
模板
{{ form_start(form) }}
{{ form_widget(form) }}
<div>
<input type="submit" value="{{ 'change_password.submit'|trans }}" />
</div>
{{ form_end(form) }}