Skip to content

Commit cf94f1b

Browse files
FIX Ensure we can set titles in the child password fields (#11926)
1 parent 055e88d commit cf94f1b

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

src/Forms/ConfirmedPasswordField.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,9 @@ public function setChildrenTitles($titles)
397397
$expectedChildren = $this->getRequireExistingPassword() ? 3 : 2;
398398
if (is_array($titles) && count($titles ?? []) === $expectedChildren) {
399399
foreach ($this->getChildren() as $field) {
400+
if ($field instanceof DatalessField) {
401+
continue;
402+
}
400403
if (isset($titles[0])) {
401404
$field->setTitle($titles[0]);
402405

@@ -496,11 +499,11 @@ public function validate(): ValidationResult
496499
}
497500
$this->beforeExtending('updateValidate', function (ValidationResult $result) {
498501
$name = $this->name;
499-
502+
500503
$this->getPasswordField()->setValue($this->value);
501504
$this->getConfirmPasswordField()->setValue($this->confirmValue);
502505
$value = $this->getPasswordField()->getValue();
503-
506+
504507
// both password-fields should be the same
505508
if ($value != $this->getConfirmPasswordField()->getValue()) {
506509
$result->addFieldError(
@@ -510,7 +513,7 @@ public function validate(): ValidationResult
510513
);
511514
return;
512515
}
513-
516+
514517
if (!$this->canBeEmpty) {
515518
// both password-fields shouldn't be empty
516519
if (!$value || !$this->getConfirmPasswordField()->getValue()) {
@@ -522,7 +525,7 @@ public function validate(): ValidationResult
522525
return;
523526
}
524527
}
525-
528+
526529
// lengths
527530
$minLength = $this->getMinLength();
528531
$maxLength = $this->getMaxLength();
@@ -561,7 +564,7 @@ public function validate(): ValidationResult
561564
return;
562565
}
563566
}
564-
567+
565568
if ($this->getRequireStrongPassword()) {
566569
$strongEnough = ConstraintValidator::validate(
567570
$value,
@@ -579,7 +582,7 @@ public function validate(): ValidationResult
579582
return;
580583
}
581584
}
582-
585+
583586
// Check if current password is valid
584587
if (!empty($value) && $this->getRequireExistingPassword()) {
585588
if (!$this->currentPasswordValue) {
@@ -593,7 +596,7 @@ public function validate(): ValidationResult
593596
);
594597
return;
595598
}
596-
599+
597600
// Check this password is valid for the current user
598601
$member = Security::getCurrentUser();
599602
if (!$member) {
@@ -607,7 +610,7 @@ public function validate(): ValidationResult
607610
);
608611
return;
609612
}
610-
613+
611614
// With a valid user and password, check the password is correct
612615
$authenticators = Security::singleton()->getApplicableAuthenticators(Authenticator::CHECK_PASSWORD);
613616
foreach ($authenticators as $authenticator) {

tests/php/Forms/ConfirmedPasswordFieldTest.php

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,38 @@ public function testSetRightTitlePropagatesToChildren()
326326
}
327327
}
328328

329-
public function testSetChildrenTitles()
329+
public static function provideSetChildrenTitles(): array
330+
{
331+
return [
332+
[true],
333+
[false],
334+
];
335+
}
336+
337+
#[DataProvider('provideSetChildrenTitles')]
338+
public function testSetChildrenTitles(bool $requireExistingPassword)
330339
{
331340
$field = new ConfirmedPasswordField('Test');
332-
$field->setRequireExistingPassword(true);
333-
$field->setChildrenTitles([
334-
'Current Password',
335-
'Password',
336-
'Confirm Password',
337-
]);
341+
$field->setRequireExistingPassword($requireExistingPassword);
342+
$setValues = [
343+
'New Password field',
344+
'Confirm Password field',
345+
];
346+
347+
if ($requireExistingPassword) {
348+
array_unshift($setValues, 'Original Password field');
349+
}
350+
351+
$field->setChildrenTitles($setValues);
338352

339-
$this->assertSame('Current Password', $field->getChildren()->shift()->Title());
340-
$this->assertSame('Password', $field->getChildren()->shift()->Title());
341-
$this->assertSame('Confirm Password', $field->getChildren()->shift()->Title());
353+
$children = $field->getChildren();
354+
$children->removeByName('Test[_PasswordStrength]');
355+
356+
if ($requireExistingPassword) {
357+
$this->assertSame('Original Password field', $children->shift()->Title());
358+
}
359+
$this->assertSame('New Password field', $children->shift()->Title());
360+
$this->assertSame('Confirm Password field', $children->shift()->Title());
342361
}
343362

344363
public function testPerformReadonlyTransformation()
@@ -604,7 +623,7 @@ public function testStrength(
604623
$expectedContentType = $expectedBody ? 'application/json' : $defaultContentType;
605624
$this->assertSame($expectedContentType, $response->getHeader('Content-Type'));
606625
}
607-
626+
608627
public static function provideDataAttributes(): array
609628
{
610629
return [

0 commit comments

Comments
 (0)