I have a password stored in a database hashed with DefaultPasswordHasher on add .
I have another action for changing the password for the loggedin user, in this form I have a field called current_password , which I need to compare with the current password value from database .
The problem is that DefaultPasswordHasher generates a different hash for every time I hash the value of the form, so this will never match the hash from the database.
Follow the confirmation code for the current_password field:
->add('current_password', 'custom', [ 'rule' => function($value, $context){ $user = $this->get($context['data']['id']); if ($user) { echo $user->password; // Current password value hashed from database echo '<br>'; echo $value; //foo echo '<br>'; echo (new DefaultPasswordHasher)->hash($value); // Here is displaying a different hash each time that I post the form // Here will never match =[ if ($user->password == (new DefaultPasswordHasher)->hash($value)) { return true; } } return false; }, 'message' => 'Você não confirmou a sua senha atual corretamente' ])
source share