diff --git a/core/lib/Drupal/Core/Datetime/Element/Datelist.php b/core/lib/Drupal/Core/Datetime/Element/Datelist.php index b560065..c68a787 100644 --- a/core/lib/Drupal/Core/Datetime/Element/Datelist.php +++ b/core/lib/Drupal/Core/Datetime/Element/Datelist.php @@ -55,7 +55,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form $date = NULL; if ($input !== FALSE) { $return = $input; - if (is_null(static::checkEmptyInputs($input, $parts))) { + if (empty(static::checkEmptyInputs($input, $parts))) { if (isset($input['ampm'])) { if ($input['ampm'] == 'pm' && $input['hour'] < 12) { $input['hour'] += 12; @@ -267,7 +267,7 @@ public static function processDatelist(&$element, FormStateInterface $form_state '#attributes' => $element['#attributes'], '#options' => $options, '#required' => $element['#required'], - '#error_no_message' => TRUE, + '#error_no_message' => FALSE, ); } @@ -302,7 +302,7 @@ public static function validateDatelist(&$element, FormStateInterface $form_stat $input_exists = FALSE; $input = NestedArray::getValue($form_state->getValues(), $element['#parents'], $input_exists); if ($input_exists) { - $first_empty = static::checkEmptyInputs($input, $element['#date_part_order']); + $all_empty = static::checkEmptyInputs($input, $element['#date_part_order']); // If there's empty input and the field is not required, set it to empty. if (empty($input['year']) && empty($input['month']) && empty($input['day']) && !$element['#required']) { @@ -312,9 +312,11 @@ public static function validateDatelist(&$element, FormStateInterface $form_stat elseif (empty($input['year']) && empty($input['month']) && empty($input['day']) && $element['#required']) { $form_state->setError($element, t('The %field date is required.')); } - elseif (!is_null($first_empty)) { - $message = t('The %field date is invalid. A value must be selected for %part.', array('%field' => !empty($element['#title']) ? $element['#title'] : '', '%part' => $first_empty)); - $form_state->setError($element, $message); + elseif (!empty($all_empty)) { + foreach ( $all_empty as $value ){ + $message = t('A value must be selected for %part.', array('%part' => $value)); + $form_state->setError($element[$value], $message); + } } else { // If the input is valid, set it. @@ -334,24 +336,25 @@ public static function validateDatelist(&$element, FormStateInterface $form_stat * Checks the input array for empty values. * * Input array keys are checked against values in the parts array. Elements - * not in the parts array are ignored. If no empty values are found, null is - * returned. Otherwise, returns the first key in the input array that has an - * empty value. + * not in the parts array are ignored. Returns an array representing elements + * from the input array that have no value. If no empty values are found, + * returned array is empty. * * @param array $input - * + * Array of individual inputs to check for value * @param array $parts - * - * @return string - * + * Array to check input against, ignoring elements not in this array + * @return array + * Array of keys from the input array that have no value, may be empty */ protected static function checkEmptyInputs($input, $parts) { + $empty_parts = []; foreach ($parts as $part) { if (empty($input[$part])) { - return $part; + $empty_parts[] = $part; } } - return null; + return $empty_parts; } /** diff --git a/core/modules/datetime/src/Tests/DateTimeFieldTest.php b/core/modules/datetime/src/Tests/DateTimeFieldTest.php index 7e02528..70ec207 100644 --- a/core/modules/datetime/src/Tests/DateTimeFieldTest.php +++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php @@ -504,7 +504,7 @@ function testDatelistWidget() { $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertResponse(200); - $this->assertText('error has been found'); + $this->assertTextPattern('/(error has)|(errors have) been found/', 'Date validation error found'); } /** diff --git a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php index 5291fc6..2592333 100644 --- a/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php +++ b/core/tests/Drupal/Tests/Component/Datetime/DateTimePlusTest.php @@ -249,22 +249,6 @@ public function testDateTimezoneWithDateTimeObject() { } /** - * Tests the checkArray method. - * - * @param mixed $expected - * Expected result of checkArray method - * @param array $date_value - * Input argument for DateTimePlus::checkArray(). - * - * @dataProvider providerTestCheckArray - * - * @covers ::checkArray - */ - public function testCheckArray($expected, $date_value) { - $this->assertEquals($expected, DateTimePlus::checkArray($date_value)); - } - - /** * Provides data for date tests. * * @return array @@ -549,32 +533,4 @@ public function providerTestDateTimestamp() { ); } - /** - * Data provider for testCheckArray(). - * - * @return array - * An array of arrays, each containing: - * - 'expected' - The expected result of DateTimePlusTest::checkArray(). - * - 'input' - Input for DateTimePlusTest::checkArray(). - * - * @see testCheckArray() - */ - public function providerTestCheckArray() { - - $data = array( - // Checks for completion of date array - array(FALSE, array('year' => 2013, 'month' => '', 'day' => '', 'hour' => '', 'minute' => '')), - array(FALSE, array('year' => 2013, 'month' => '12', 'day' => '', 'hour' => '', 'minute' => '')), - array(FALSE, array('year' => 2013, 'month' => '12', 'day' => '11', 'hour' => '', 'minute' => '')), - array(FALSE, array('year' => 2013, 'month' => '12', 'day' => '11', 'hour' => '10', 'minute' => '')), - array(TRUE, array('year' => 2013, 'month' => '12', 'day' => '11', 'hour' => '10', 'minute' => '09')), - - // Checks for invalid time pairings - array(FALSE, array('year' => 2013, 'month' => '12', 'day' => '11', 'hour' => '10', 'minute' => '61')), - array(FALSE, array('year' => 2013, 'month' => '12', 'day' => '11', 'hour' => '25', 'minute' => '09')), - ); - - return $data; - } - }