Browse Source

Mention the valid formats in the date/time data validation error message

ravage84 2 years ago
parent
commit
396d06bf03
2 changed files with 20 additions and 3 deletions
  1. 11 2
      src/Validation/Validator.php
  2. 9 1
      tests/TestCase/Validation/ValidatorTest.php

+ 11 - 2
src/Validation/Validator.php

@@ -1835,11 +1835,20 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         ?string $message = null,
         Closure|string|null $when = null
     ) {
+        $formatEnumeration = implode(', ', $formats);
+
         if ($message === null) {
             if (!$this->_useI18n) {
-                $message = 'The provided value must be a date and time';
+                $message = sprintf(
+                    'The provided value must be a date and time of one of these formats: `%s`',
+                    $formatEnumeration
+                );
             } else {
-                $message = __d('cake', 'The provided value must be a date and time');
+                $message = __d(
+                    'cake',
+                    'The provided value must be a date and time of one of these formats: `{0}`',
+                    $formatEnumeration
+                );
             }
         }
 

+ 9 - 1
tests/TestCase/Validation/ValidatorTest.php

@@ -2255,9 +2255,17 @@ class ValidatorTest extends TestCase
 
         $fieldName = 'field_name';
         $rule = 'dateTime';
-        $expectedMessage = 'The provided value must be a date and time';
+        $expectedMessage = 'The provided value must be a date and time of one of these formats: `ymd`';
         $format = ['ymd'];
         $this->assertValidationMessage($fieldName, $rule, $expectedMessage, $format);
+
+        $format = null;
+        // Same message expected
+        $this->assertValidationMessage($fieldName, $rule, $expectedMessage, $format);
+
+        $expectedMessage = 'The provided value must be a date and time of one of these formats: `ymd, dmy`';
+        $format = ['ymd', 'dmy'];
+        $this->assertValidationMessage($fieldName, $rule, $expectedMessage, $format);
     }
 
     /**