diff --git a/src/Symfony/Component/Form/Extension/PasswordHasher/EventListener/PasswordHasherListener.php b/src/Symfony/Component/Form/Extension/PasswordHasher/EventListener/PasswordHasherListener.php index 3fccfd8965810..bb74e7792e2f5 100644 --- a/src/Symfony/Component/Form/Extension/PasswordHasher/EventListener/PasswordHasherListener.php +++ b/src/Symfony/Component/Form/Extension/PasswordHasher/EventListener/PasswordHasherListener.php @@ -37,6 +37,10 @@ public function __construct( public function registerPassword(FormEvent $event) { + if (null === $event->getData() || '' === $event->getData()) { + return; + } + $this->assertNotMapped($event->getForm()); $this->passwords[] = [ diff --git a/src/Symfony/Component/Form/Tests/Extension/PasswordHasher/Type/PasswordTypePasswordHasherExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/PasswordHasher/Type/PasswordTypePasswordHasherExtensionTest.php index 895a39c41b45d..0b745c172f0f2 100644 --- a/src/Symfony/Component/Form/Tests/Extension/PasswordHasher/Type/PasswordTypePasswordHasherExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/PasswordHasher/Type/PasswordTypePasswordHasherExtensionTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\Form\Exception\InvalidConfigurationException; +use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\Extension\Core\Type\PasswordType; use Symfony\Component\Form\Extension\Core\Type\RepeatedType; use Symfony\Component\Form\Extension\PasswordHasher\EventListener\PasswordHasherListener; @@ -80,6 +81,36 @@ public function testPasswordHashSuccess() $this->assertSame($user->getPassword(), $hashedPassword); } + public function testPasswordHashSkippedWithEmptyPassword() + { + $oldHashedPassword = 'PreviousHashedPassword'; + + $user = new User(); + $user->setPassword($oldHashedPassword); + + $this->passwordHasher + ->expects($this->never()) + ->method('hashPassword') + ; + + $this->assertEquals($user->getPassword(), $oldHashedPassword); + + $form = $this->factory + ->createBuilder(FormType::class, $user) + ->add('plainPassword', PasswordType::class, [ + 'hash_property_path' => 'password', + 'mapped' => false, + 'required' => false, + ]) + ->getForm() + ; + + $form->submit(['plainPassword' => '']); + + $this->assertTrue($form->isValid()); + $this->assertSame($user->getPassword(), $oldHashedPassword); + } + public function testPasswordHashSuccessWithEmptyData() { $user = new User();