I am trying to build a form that resets user password. I am using FOSUserBundle to manage users, but I don't want to override FOSUser resetting controller due to some architecture reasons
So I decided to build my own type and controller to reset password
PasswordResettingType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'attr' => ['class' => 'form-group has-feedback'],
'first_options' => array('label' => false,
'attr' => ['placeholder' => 'New Password']
),
'second_options' => array('label' => false,
'attr' => ['placeholder' => 'Repeat Password']),
'invalid_message' => 'Passwords don't match',
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CoreBundle\Entity\User',
'csrf_token_id' => 'resetting'
));
}
Resetting controller
/**
* @Route("/reset/{token}", name="api_resetting_reset")
*/
public function resetAction(Request $request, $token)
{
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->findUserByConfirmationToken($token);
if (null === $user) {
return $this->render('APIBundle:Resetting:error.html.twig');
}
$form = $this->createForm(PasswordResettingType::class, $user);
$form->handleRequest($request);
if ($form->isValid()) {
$user->setConfirmationToken(null);
$user->setPasswordRequestedAt(null);
$user->setPlainPassword($form["plainPassword"]->getData());
$userManager->updateUser($user);
return $this->redirectToRoute('api_resetting_success');
}
return $this->render('APIBundle:Resetting:reset.html.twig', array(
'token' => $token,
'form' => $form->createView()
));
}
reset.html.twig
{{ form_start(form) }}
{% for passwordField in form.plainPassword %}
<div class="form-group has-feedback">
{{ form_widget(passwordField, { 'attr': {'class': 'form-control'} }) }}
<span class="show">show</span>
{{ form_errors(passwordField) }}
</div>
{% endfor %}
<input type="submit" class="btn" value="Submit" />
{{ form_end(form) }}
But when I submit form, new password is not set, ConfirmationToken and PasswordRequestedAt are not set to null.
/reset/{token}route for example). I think you would have notice if that was the case, but like everyone here, I can't see why this wouldn't work.