What is the best way to implement change password functionality in Symfony2? Right now I'm using this:
$builder->add('password', 'repeated', array(
'first_name' => 'New password',
'second_name' => 'Confirm new password',
'type' => 'password'
));
It should also contain the current password check for security reasons.
Note: I'm not using FOSUserBundle
.
Since Symfony 2.3 you can easily use
UserPassword
validation constraint.Acme\UserBundle\Form\Model\ChangePassword.php
Acme\UserBundle\Form\ChangePasswordType.php
Acme\UserBundle\Controller\DemoController.php
I use a action from my controller:
Can't You get old password from User before binding form?
You have to either create another model with two fields:
Or add a non-persisted property to your user model like the FOSUserBundle does (see the
plainPassword
property).So once you checked both current and new password are valid, you encode the new password and replace the old one with it.
Just add this to your form type: