I have a problem with the typical form to change the password in which I first put you to put your current password.
The theory is very simple and is explained in the symfony doc but it gives me an error like the current password is incorrect
My Model:
namespace BackendBundle\Form\Model;
use Symfony\Component\Security\Core\Validator\Constraints as SecurityAssert;
use Symfony\Component\Validator\Constraints as Assert;
class ChangePassword
{
/**
* @SecurityAssert\UserPassword(
* message = "Error al poner la contraseña actual"
* )
*/
protected $oldPassword;
/**
* @Assert\Length(
* min = 6,
* minMessage = "El password tiene que tener al menos 6 caracteres"
* )
*/
protected $password;
/////
public function getOldPassword() {
return $this->oldPassword;
}
public function setOldPassword($oldPassword) {
$this->oldPassword = $oldPassword;
}
/////
public function getPassword() {
return $this->oldPassword;
}
public function setPassword($oldPassword) {
$this->oldPassword = $oldPassword;
}
}
The fomType:
->add('oldPassword', PasswordType::class, array(
'label' => 'Ponga su password actual',
'mapped' => false,
))
->add('password', RepeatedType::class, array(
"required" => "required",
'type' => PasswordType::class,
'invalid_message' => 'Los dos password deben coincidir',
'first_options' => array('label' => 'Password nuevo', "attr"
=> array("class" => "form-password form-control")),
'second_options' => array('label' => 'Repita el Password nuevo', "attr" => array("class" => "form-password form-control"))
)
)
And little else must be done (I think) in addition to the controler create the view with the form and then collect the data of the new password, etc, but as I say, I sent error of not valid field that checks the password and I do not know if it's because the password I keep it encrypted My security.yml I have it as
encoders:
BackendBundle\Entity\Users:
algorithm: bcrypt
cost: 6
My action Controller:
public function passAction(Request $request) {
$changePasswordModel = new ChangePassword();
$form = $this->createForm(ChangePasswordType::class, $changePasswordModel);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$user = $this->getUser(); //metemos como id la del usuario sacado de su sesion
$encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
$password = $encoder->encodePassword($changePasswordModel->getPassword(), $user->getSalt());
$user->setPassword($password);
$em->persist($user);
$flush = $em->flush();
if ($flush === null) {
$this->session->getFlashBag()->add('success', 'El usuario se ha editado correctamente');
return $this->redirectToRoute("others_show"); //redirigimos la pagina si se incluido correctamete
} else {
$this->session->getFlashBag()->add('warning', 'Error al editar el password');
}
} else {
dump($form->getErrors());
$this->session->getFlashBag()->add('warning', 'El password no se ha editado por un error en el formulario !');
}
}
return $this->render('BackendBundle:Others:editPass.html.twig', array(
'form' => $form->createView(),
));
}
It tells me that the form is not correct because the old password is incorrect. What could be wrong?