浏览代码

merged branch ondrowan/2.0 (PR #2614)

Commits
-------

2582fcb Added tests for string fix in DateTimeToArrayTransformer (8351a112861cd1cc71fbe35d69455c504d2f6acf).
8351a11 Added check for array fields to be integers in reverseTransform method. This prevents checkdate from getting strings as arguments and throwing incorrect ErrorException when submitting form with malformed (string) data in, for example, Date field. #2609

Discussion
----------

Fix for #2609

Second take for fix for #2609, hope it's ok now. Tests are failing without my fix and passing with it.
Fabien Potencier 13 年之前
父节点
当前提交
7475a3924c

+ 4 - 0
src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php

@@ -142,6 +142,10 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
             ));
         }
 
+        if (preg_match( '/^\d*$/', $value['month'] . $value['day'] . $value['year']) === 0) {
+            throw new TransformationFailedException('This is an invalid date');
+        }
+
         if (!empty($value['month']) && !empty($value['day']) && !empty($value['year']) && false === checkdate($value['month'], $value['day'], $value['year'])) {
             throw new TransformationFailedException('This is an invalid date');
         }

+ 48 - 0
tests/Symfony/Tests/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformerTest.php

@@ -463,4 +463,52 @@ class DateTimeToArrayTransformerTest extends DateTimeTestCase
             'second' => '6',
         ));
     }
+
+    /**
+     * @expectedException Symfony\Component\Form\Exception\TransformationFailedException
+     */
+    public function testReverseTransformWithStringDay()
+    {
+        $transformer = new DateTimeToArrayTransformer();
+        $transformer->reverseTransform(array(
+            'year' => '2010',
+            'month' => '2',
+            'day' => 'bazinga',
+            'hour' => '4',
+            'minute' => '5',
+            'second' => '6',
+        ));
+    }
+
+    /**
+     * @expectedException Symfony\Component\Form\Exception\TransformationFailedException
+     */
+    public function testReverseTransformWithStringMonth()
+    {
+        $transformer = new DateTimeToArrayTransformer();
+        $transformer->reverseTransform(array(
+            'year' => '2010',
+            'month' => 'bazinga',
+            'day' => '31',
+            'hour' => '4',
+            'minute' => '5',
+            'second' => '6',
+        ));
+    }
+
+    /**
+     * @expectedException Symfony\Component\Form\Exception\TransformationFailedException
+     */
+    public function testReverseTransformWithStringYear()
+    {
+        $transformer = new DateTimeToArrayTransformer();
+        $transformer->reverseTransform(array(
+            'year' => 'bazinga',
+            'month' => '2',
+            'day' => '31',
+            'hour' => '4',
+            'minute' => '5',
+            'second' => '6',
+        ));
+    }
 }