浏览代码

Merge pull request #5821 from cakephp/empty-date-marshal

Empty date marshal & incorrect single inputs.
Mark Story 11 年之前
父节点
当前提交
41ae5fbd53

+ 3 - 0
src/Database/Type/DateTimeType.php

@@ -135,6 +135,9 @@ class DateTimeType extends \Cake\Database\Type
             return $value;
         }
 
+        if (is_array($value) && implode('', $value) === '') {
+            return null;
+        }
         $value += ['hour' => 0, 'minute' => 0, 'second' => 0];
 
         $format = '';

+ 1 - 2
src/View/Helper/FormHelper.php

@@ -2143,9 +2143,8 @@ class FormHelper extends Helper
                 $options[$type] = [];
             }
 
-            // Pass empty boolean to each type.
+            // Pass empty options to each type.
             if (!empty($options['empty']) &&
-                is_bool($options['empty']) &&
                 is_array($options[$type])
             ) {
                 $options[$type]['empty'] = $options['empty'];

+ 4 - 0
tests/TestCase/Database/Type/DateTimeTypeTest.php

@@ -116,6 +116,10 @@ class DateTimeTypeTest extends TestCase
 
             // valid array types
             [
+                ['year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => ''],
+                null
+            ],
+            [
                 ['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
                 new Time('2014-02-14 13:14:15')
             ],

+ 4 - 0
tests/TestCase/Database/Type/DateTypeTest.php

@@ -112,6 +112,10 @@ class DateTypeTest extends TestCase
 
             // valid array types
             [
+                ['year' => '', 'month' => '', 'day' => ''],
+                null,
+            ],
+            [
                 ['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
                 new Time('2014-02-14 00:00:00')
             ],

+ 8 - 0
tests/TestCase/Database/Type/TimeTypeTest.php

@@ -111,6 +111,14 @@ class TimeTypeTest extends TestCase
 
             // valid array types
             [
+                ['hour' => '', 'minute' => '', 'second' => ''],
+                null,
+            ],
+            [
+                ['hour' => '', 'minute' => '', 'meridian' => ''],
+                null,
+            ],
+            [
                 ['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
                 new Time('2014-02-14 13:14:15')
             ],

+ 15 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -5178,6 +5178,11 @@ class FormHelperTest extends TestCase
             '*/select',
         ];
         $this->assertHtml($expected, $result);
+
+        $result = $this->Form->month('Contact.published', [
+            'empty' => 'Published on',
+        ]);
+        $this->assertContains('Published on', $result);
     }
 
     /**
@@ -5268,6 +5273,11 @@ class FormHelperTest extends TestCase
             '/select',
         ];
         $this->assertHtml($expected, $result);
+
+        $result = $this->Form->day('Contact.published', [
+            'empty' => 'Published on',
+        ]);
+        $this->assertContains('Published on', $result);
     }
 
     /**
@@ -5545,6 +5555,11 @@ class FormHelperTest extends TestCase
             '/select',
         ];
         $this->assertHtml($expected, $result);
+
+        $result = $this->Form->year('Contact.published', [
+            'empty' => 'Published on',
+        ]);
+        $this->assertContains('Published on', $result);
     }
 
     /**