From 1aaa480b07695ccbe91741aa721fd396a8a8b77b Mon Sep 17 00:00:00 2001 From: Sally Young <git@justa.fish> Date: Thu, 18 May 2023 12:28:10 +0100 Subject: [PATCH] Issue #3354606 by ranjith_kumar_k_u, pascalim, Abhijith S, smustgrave, borisson_: Datetime field name missing from validation error message --- core/lib/Drupal/Core/Datetime/Element/Datetime.php | 2 +- .../tests/src/Functional/DateTimeFieldTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/core/lib/Drupal/Core/Datetime/Element/Datetime.php b/core/lib/Drupal/Core/Datetime/Element/Datetime.php index 0a7bceb808c7..f035e16bf648 100644 --- a/core/lib/Drupal/Core/Datetime/Element/Datetime.php +++ b/core/lib/Drupal/Core/Datetime/Element/Datetime.php @@ -351,7 +351,7 @@ public static function validateDatetime(&$element, FormStateInterface $form_stat $input = NestedArray::getValue($form_state->getValues(), $element['#parents'], $input_exists); if ($input_exists) { - $title = !empty($element['#title']) ? $element['#title'] : ''; + $title = static::getElementTitle($element, $complete_form); $date_format = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : ''; $time_format = $element['#date_time_element'] != 'none' ? static::getHtml5TimeFormat($element) : ''; $format = trim($date_format . ' ' . $time_format); diff --git a/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php b/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php index 70437f910667..968e557fcb24 100644 --- a/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php +++ b/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php @@ -10,6 +10,7 @@ use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\node\Entity\Node; +use Drupal\Core\Form\FormState; /** * Tests Datetime field functionality. @@ -385,6 +386,19 @@ public function testDatetimeField() { ]); $output = $this->renderTestEntity($id); $this->assertStringContainsString((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected])); + + // Test the required field validation error message. + $entity = EntityTest::create(['name' => 'test datetime required message']); + $form = \Drupal::entityTypeManager()->getFormObject('entity_test', 'default')->setEntity($entity); + $form_state = new FormState(); + \Drupal::formBuilder()->submitForm($form, $form_state); + $errors = $form_state->getErrors(); + $arguments = $errors["{$field_name}][0][value"]->getArguments(); + $expected_error_message = new FormattableMarkup('The %field date is required. Please enter a date in the format %format.', ['%field' => $field_label, '%format' => $arguments['%format']]); + $actual_error_message = $errors["{$field_name}][0][value"]->__toString(); + $this->assertEquals($expected_error_message->__toString(), $actual_error_message); + // Verify the format value is in the "YYYY-MM-DD HH:MM:SS" format. + $this->assertMatchesRegularExpression('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', $arguments['%format']); } /** -- GitLab