From 654156d46d109e1cca0198c069aa6b657b574aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sam=20Minn=C3=A9e?= Date: Mon, 10 Jun 2019 15:48:29 +1200 Subject: [PATCH] FIX: Fix bug when confirmed password is changed but not the password. (#9012) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In this case the confirmed password field is not reflected. It’s unclear how often this situation would arise outside of test scenarios, but may come up if $form->loadDataFrom() is called more than once. Fixes #2496 (it’s a minor issue but I think this is why Dan flagged it as a regression). Originally introduced as part of Dan’s initial fix at 2a6f1f1949956b4c91c5b7925707f29653dc1033. --- src/Forms/ConfirmedPasswordField.php | 4 ++- .../php/Forms/ConfirmedPasswordFieldTest.php | 26 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Forms/ConfirmedPasswordField.php b/src/Forms/ConfirmedPasswordField.php index dd022be49..42de7ff29 100644 --- a/src/Forms/ConfirmedPasswordField.php +++ b/src/Forms/ConfirmedPasswordField.php @@ -341,6 +341,7 @@ class ConfirmedPasswordField extends FormField //store this for later $oldValue = $this->value; + $oldConfirmValue = $this->confirmValue; if (is_array($value)) { $this->value = $value['_Password']; @@ -364,7 +365,8 @@ class ConfirmedPasswordField extends FormField if ($oldValue != $this->value) { $this->getChildren()->fieldByName($this->getName() . '[_Password]') ->setValue($this->value); - + } + if ($oldConfirmValue != $this->confirmValue) { $this->getChildren()->fieldByName($this->getName() . '[_ConfirmPassword]') ->setValue($this->confirmValue); } diff --git a/tests/php/Forms/ConfirmedPasswordFieldTest.php b/tests/php/Forms/ConfirmedPasswordFieldTest.php index e4283c42e..2f4face19 100644 --- a/tests/php/Forms/ConfirmedPasswordFieldTest.php +++ b/tests/php/Forms/ConfirmedPasswordFieldTest.php @@ -148,12 +148,34 @@ class ConfirmedPasswordFieldTest extends SapphireTest $form->loadDataFrom([ 'Password' => [ '_Password' => '123', - '_ConfirmPassword' => '999', + '_ConfirmPassword' => '', ], ]); $this->assertEquals('123', $field->children->first()->Value()); - $this->assertEquals('999', $field->children->last()->Value()); + $this->assertEmpty($field->children->last()->Value()); + $this->assertNotEquals($field->children->first()->Value(), $field->children->last()->Value()); + + $form->loadDataFrom([ + 'Password' => [ + '_Password' => '123', + '_ConfirmPassword' => 'abc', + ], + ]); + + $this->assertEquals('123', $field->children->first()->Value()); + $this->assertEquals('abc', $field->children->last()->Value()); + $this->assertNotEquals($field->children->first()->Value(), $field->children->last()->Value()); + + $form->loadDataFrom([ + 'Password' => [ + '_Password' => '', + '_ConfirmPassword' => 'abc', + ], + ]); + + $this->assertEmpty($field->children->first()->Value()); + $this->assertEquals('abc', $field->children->last()->Value()); $this->assertNotEquals($field->children->first()->Value(), $field->children->last()->Value()); }