diff --git a/core/modules/field_ui/src/Form/FieldStorageAddForm.php b/core/modules/field_ui/src/Form/FieldStorageAddForm.php index 560e8d7c726..8c989318408 100644 --- a/core/modules/field_ui/src/Form/FieldStorageAddForm.php +++ b/core/modules/field_ui/src/Form/FieldStorageAddForm.php @@ -381,24 +381,21 @@ class FieldStorageAddForm extends FormBase { */ protected function validateAddNew(array $form, FormStateInterface $form_state) { // Validate if any information was provided in the 'add new field' case. - if ($form_state->getValue('new_storage_type')) { - // Missing label. - if (!$form_state->getValue('label')) { - $form_state->setErrorByName('label', $this->t('Add new field: you need to provide a label.')); - } + // Missing label. + if (!$form_state->getValue('label')) { + $form_state->setErrorByName('label', $this->t('Add new field: you need to provide a label.')); + } + // Missing field name. + if (!$form_state->getValue('field_name')) { + $form_state->setErrorByName('field_name', $this->t('Add new field: you need to provide a machine name for the field.')); + } + // Field name validation. + else { + $field_name = $form_state->getValue('field_name'); - // Missing field name. - if (!$form_state->getValue('field_name')) { - $form_state->setErrorByName('field_name', $this->t('Add new field: you need to provide a machine name for the field.')); - } - // Field name validation. - else { - $field_name = $form_state->getValue('field_name'); - - // Add the field prefix. - $field_name = $this->configFactory->get('field_ui.settings')->get('field_prefix') . $field_name; - $form_state->setValueForElement($form['new_storage_wrapper']['field_name'], $field_name); - } + // Add the field prefix. + $field_name = $this->configFactory->get('field_ui.settings')->get('field_prefix') . $field_name; + $form_state->setValueForElement($form['new_storage_wrapper']['field_name'], $field_name); } } diff --git a/core/modules/field_ui/tests/src/FunctionalJavascript/ManageFieldsTest.php b/core/modules/field_ui/tests/src/FunctionalJavascript/ManageFieldsTest.php index 7918e0eb2ac..9261839e2ec 100644 --- a/core/modules/field_ui/tests/src/FunctionalJavascript/ManageFieldsTest.php +++ b/core/modules/field_ui/tests/src/FunctionalJavascript/ManageFieldsTest.php @@ -348,4 +348,16 @@ class ManageFieldsTest extends WebDriverTestBase { $this->assertSession()->pageTextContains('Limit must be higher than or equal to 1.'); } + /** + * Tests the form validation for label field. + */ + public function testLabelFieldFormValidation() { + $this->drupalGet('/admin/structure/types/manage/article/fields/add-field'); + $page = $this->getSession()->getPage(); + $page->findButton('Continue')->click(); + + $this->assertSession()->pageTextContains('You need to provide a label.'); + $this->assertSession()->pageTextContains('You need to select a field type.'); + } + }