From f373ed22b0d39e1db57ff5b255b8c864bf541d03 Mon Sep 17 00:00:00 2001
From: "Ravi.J" <21369-julaboy@users.noreply.drupalcode.org>
Date: Thu, 29 Jun 2023 14:54:05 +0200
Subject: [PATCH] Issue #1344552 by marcingy, Niklas Fiekas, Ravi.J, aleevas,
Eduardo Morales Alberti: Password confirm field type should have support for
#maxlength attribute
---
.../Core/Render/Element/PasswordConfirm.php | 25 +++++++++----------
.../Kernel/Form/FormElementMaxlengthTest.php | 14 +++++++++++
.../Tests/Core/Form/FormValidatorTest.php | 9 +++++++
3 files changed, 35 insertions(+), 13 deletions(-)
diff --git a/core/lib/Drupal/Core/Render/Element/PasswordConfirm.php b/core/lib/Drupal/Core/Render/Element/PasswordConfirm.php
index c0f956cbd519..a1196eff8bce 100644
--- a/core/lib/Drupal/Core/Render/Element/PasswordConfirm.php
+++ b/core/lib/Drupal/Core/Render/Element/PasswordConfirm.php
@@ -51,16 +51,12 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
$element += ['#default_value' => []];
return $element['#default_value'] + ['pass1' => '', 'pass2' => ''];
}
- $value = ['pass1' => '', 'pass2' => ''];
- // Throw out all invalid array keys; we only allow pass1 and pass2.
- foreach ($value as $allowed_key => $default) {
- // These should be strings, but allow other scalars since they might be
- // valid input in programmatic form submissions. Any nested array values
- // are ignored.
- if (isset($input[$allowed_key]) && is_scalar($input[$allowed_key])) {
- $value[$allowed_key] = (string) $input[$allowed_key];
- }
+
+ $value = '';
+ if (isset($input['pass1']) && is_scalar($input['pass1'])) {
+ $value = (string) $input['pass1'];
}
+
return $value;
}
@@ -68,10 +64,10 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
* Expand a password_confirm field into two text boxes.
*/
public static function processPasswordConfirm(&$element, FormStateInterface $form_state, &$complete_form) {
+
$element['pass1'] = [
'#type' => 'password',
'#title' => t('Password'),
- '#value' => empty($element['#value']) ? NULL : $element['#value']['pass1'],
'#required' => $element['#required'],
'#attributes' => [
'class' => ['password-field', 'js-password-field'],
@@ -82,7 +78,6 @@ public static function processPasswordConfirm(&$element, FormStateInterface $for
$element['pass2'] = [
'#type' => 'password',
'#title' => t('Confirm password'),
- '#value' => empty($element['#value']) ? NULL : $element['#value']['pass2'],
'#required' => $element['#required'],
'#attributes' => [
'class' => ['password-confirm', 'js-password-confirm'],
@@ -97,6 +92,10 @@ public static function processPasswordConfirm(&$element, FormStateInterface $for
$element['pass1']['#size'] = $element['pass2']['#size'] = $element['#size'];
}
+ if (isset($element['#maxlength'])) {
+ $element['pass1']['#maxlength'] = $element['pass2']['#maxlength'] = $element['#maxlength'];
+ }
+
return $element;
}
@@ -104,8 +103,8 @@ public static function processPasswordConfirm(&$element, FormStateInterface $for
* Validates a password_confirm element.
*/
public static function validatePasswordConfirm(&$element, FormStateInterface $form_state, &$complete_form) {
- $pass1 = trim($element['pass1']['#value']);
- $pass2 = trim($element['pass2']['#value']);
+ $pass1 = trim($form_state->getValue('password')['pass1']);
+ $pass2 = trim($form_state->getValue('password')['pass2']);
if (strlen($pass1) > 0 || strlen($pass2) > 0) {
if (strcmp($pass1, $pass2)) {
$form_state->setError($element, t('The specified passwords do not match.'));
diff --git a/core/modules/system/tests/src/Kernel/Form/FormElementMaxlengthTest.php b/core/modules/system/tests/src/Kernel/Form/FormElementMaxlengthTest.php
index 480ccb3b30b1..224a6ad63d02 100644
--- a/core/modules/system/tests/src/Kernel/Form/FormElementMaxlengthTest.php
+++ b/core/modules/system/tests/src/Kernel/Form/FormElementMaxlengthTest.php
@@ -41,6 +41,16 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#maxlength' => 255,
];
+ $form['pass1'] = [
+ '#type' => 'password',
+ '#maxlength' => 12,
+ ];
+
+ $form['pass2'] = [
+ '#type' => 'password',
+ '#maxlength' => 12,
+ ];
+
$form['submit'] = [
'#type' => 'submit',
'#value' => 'Submit',
@@ -75,6 +85,10 @@ public function testAttributes() {
$this->assertCount(1, $elements, 'Text field has correct maxlength in form.');
$elements = $this->xpath($css_selector_converter->toXPath('textarea[name=description][maxlength=255]'));
$this->assertCount(1, $elements, 'Textarea field has correct maxlength in form.');
+ $elements = $this->xpath($css_selector_converter->toXPath('input[name=pass1][maxlength=12]'));
+ $this->assertCount(1, $elements, 'First password field has correct maxlength in form.');
+ $elements = $this->xpath($css_selector_converter->toXPath('input[name=pass2][maxlength=12]'));
+ $this->assertCount(1, $elements, 'Second password field has correct maxlength in form.');
}
}
diff --git a/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php b/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php
index 4190834d19c1..8c970e0bea53 100644
--- a/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php
+++ b/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php
@@ -463,6 +463,15 @@ public static function providerTestPerformRequiredValidation() {
'Test cannot be longer than 7 characters but is currently 8 characters long.',
FALSE,
],
+ [
+ [
+ '#type' => 'password',
+ '#maxlength' => 12,
+ '#value' => $this->randomMachineName(13),
+ ],
+ 'Test cannot be longer than 12 characters but is currently 13 characters long.',
+ FALSE,
+ ],
];
}
--
GitLab