diff --git a/core/lib/Drupal/Core/Render/Element/MachineName.php b/core/lib/Drupal/Core/Render/Element/MachineName.php index 762509f6d4..4ed3ebf612 100644 --- a/core/lib/Drupal/Core/Render/Element/MachineName.php +++ b/core/lib/Drupal/Core/Render/Element/MachineName.php @@ -155,11 +155,11 @@ public static function processMachineName(&$element, FormStateInterface $form_st // ensure that the exists function is not called for existing values when // editing them. $initial_values = $form_state->get('machine_name.initial_values') ?: []; - $element_path = implode('][', $element['#parents']); + $element_key = implode('-', $element['#array_parents']); // Store the initial values in an array so we can differentiate between a // NULL default value and a new machine name element. - if (!array_key_exists($element_path, $initial_values)) { - $initial_values[$element_path] = $element['#default_value']; + if (!array_key_exists($element_key, $initial_values)) { + $initial_values[$element_key] = $element['#default_value']; $form_state->set('machine_name.initial_values', $initial_values); } @@ -263,8 +263,8 @@ public static function validateMachineName(&$element, FormStateInterface $form_s // default value then it does not need to be validated as the machine name // element assumes the form is editing the existing value. $initial_values = $form_state->get('machine_name.initial_values') ?: []; - $element_path = implode('][', $element['#parents']); - if (!array_key_exists($element_path, $initial_values) || $initial_values[$element_path] !== $element['#value']) { + $element_key = implode('-', $element['#array_parents']); + if (!array_key_exists($element_key, $initial_values) || $initial_values[$element_key] !== $element['#value']) { $function = $element['#machine_name']['exists']; if (call_user_func($function, $element['#value'], $element, $form_state)) { $form_state->setError($element, t('The machine-readable name is already in use. It must be unique.')); diff --git a/core/modules/system/tests/src/FunctionalJavascript/MachineNameValidationTest.php b/core/modules/system/tests/src/FunctionalJavascript/MachineNameValidationTest.php deleted file mode 100644 index e09ca6cf4c..0000000000 --- a/core/modules/system/tests/src/FunctionalJavascript/MachineNameValidationTest.php +++ /dev/null @@ -1,57 +0,0 @@ -assertSession(); - $this->drupalGet('/form-test/form-test-machine-name-validation'); - - // Test errors after with no AJAX. - $assert->buttonExists('Save')->press(); - $assert->pageTextContains('Machine-readable name field is required.'); - // Ensure only the first machine name field has an error. - $this->assertTrue($assert->fieldExists('id')->hasClass('error')); - $this->assertFalse($assert->fieldExists('id2')->hasClass('error')); - - // Test a successful submit after using AJAX. - $assert->fieldExists('Name')->setValue('test 1'); - $assert->fieldExists('id')->setValue('test_1'); - $assert->selectExists('snack')->selectOption('apple'); - $assert->assertWaitOnAjaxRequest(); - $assert->buttonExists('Save')->press(); - $assert->pageTextContains('The form_test_machine_name_validation_form form has been submitted successfully.'); - - // Test errors after using AJAX. - $assert->fieldExists('Name')->setValue('duplicate'); - $this->assertJsCondition('document.forms[0].id.value === "duplicate"'); - $assert->fieldExists('id2')->setValue('duplicate2'); - $assert->selectExists('snack')->selectOption('potato'); - $assert->assertWaitOnAjaxRequest(); - $assert->buttonExists('Save')->press(); - $assert->pageTextContains('The machine-readable name is already in use. It must be unique.'); - // Ensure both machine name fields both have errors. - $this->assertTrue($assert->fieldExists('id')->hasClass('error')); - $this->assertTrue($assert->fieldExists('id2')->hasClass('error')); - - } - -} diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php index bf2643da6a..acbaadb698 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php @@ -59,7 +59,6 @@ public function testMachineName() { // Get page and session. $page = $this->getSession()->getPage(); - $assert_session = $this->assertSession(); // Get elements from the page. $title_1 = $page->findField('machine_name_1_label'); @@ -111,6 +110,36 @@ public function testMachineName() { // Validate if the element contains the correct value. $this->assertEquals($test_values[1]['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name'); + + $assert = $this->assertSession(); + $this->drupalGet('/form-test/form-test-machine-name-validation'); + + // Test errors after with no AJAX. + $assert->buttonExists('Save')->press(); + $assert->pageTextContains('Machine-readable name field is required.'); + // Ensure only the first machine name field has an error. + $this->assertTrue($assert->fieldExists('id')->hasClass('error')); + $this->assertFalse($assert->fieldExists('id2')->hasClass('error')); + + // Test a successful submit after using AJAX. + $assert->fieldExists('Name')->setValue('test 1'); + $assert->fieldExists('id')->setValue('test_1'); + $assert->selectExists('snack')->selectOption('apple'); + $assert->assertWaitOnAjaxRequest(); + $assert->buttonExists('Save')->press(); + $assert->pageTextContains('The form_test_machine_name_validation_form form has been submitted successfully.'); + + // Test errors after using AJAX. + $assert->fieldExists('Name')->setValue('duplicate'); + $this->assertJsCondition('document.forms[0].id.value === "duplicate"'); + $assert->fieldExists('id2')->setValue('duplicate2'); + $assert->selectExists('snack')->selectOption('potato'); + $assert->assertWaitOnAjaxRequest(); + $assert->buttonExists('Save')->press(); + $assert->pageTextContains('The machine-readable name is already in use. It must be unique.'); + // Ensure both machine name fields both have errors. + $this->assertTrue($assert->fieldExists('id')->hasClass('error')); + $this->assertTrue($assert->fieldExists('id2')->hasClass('error')); } } diff --git a/core/tests/Drupal/Tests/Core/Render/Element/MachineNameTest.php b/core/tests/Drupal/Tests/Core/Render/Element/MachineNameTest.php index 3cbbab0ac2..794f2b129d 100644 --- a/core/tests/Drupal/Tests/Core/Render/Element/MachineNameTest.php +++ b/core/tests/Drupal/Tests/Core/Render/Element/MachineNameTest.php @@ -62,7 +62,7 @@ public function testProcessMachineName() { ], // The process function requires these to be set. During regular form // building they are always set. - '#parents' => ['test_machine_name'], + '#array_parents' => ['test_machine_name'], '#default_value' => NULL, ];