Skip to content

Commit 7475a39

Browse files
committed
merged branch ondrowan/2.0 (PR symfony#2614)
Commits ------- 2582fcb Added tests for string fix in DateTimeToArrayTransformer (8351a11). 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. symfony#2609 Discussion ---------- Fix for symfony#2609 Second take for fix for symfony#2609, hope it's ok now. Tests are failing without my fix and passing with it.
2 parents 1a2b3cf + 2582fcb commit 7475a39

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformer.php

+4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ public function reverseTransform($value)
142142
));
143143
}
144144

145+
if (preg_match( '/^\d*$/', $value['month'] . $value['day'] . $value['year']) === 0) {
146+
throw new TransformationFailedException('This is an invalid date');
147+
}
148+
145149
if (!empty($value['month']) && !empty($value['day']) && !empty($value['year']) && false === checkdate($value['month'], $value['day'], $value['year'])) {
146150
throw new TransformationFailedException('This is an invalid date');
147151
}

tests/Symfony/Tests/Component/Form/Extension/Core/DataTransformer/DateTimeToArrayTransformerTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -463,4 +463,52 @@ public function testReverseTransformWithInvalidDay()
463463
'second' => '6',
464464
));
465465
}
466+
467+
/**
468+
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException
469+
*/
470+
public function testReverseTransformWithStringDay()
471+
{
472+
$transformer = new DateTimeToArrayTransformer();
473+
$transformer->reverseTransform(array(
474+
'year' => '2010',
475+
'month' => '2',
476+
'day' => 'bazinga',
477+
'hour' => '4',
478+
'minute' => '5',
479+
'second' => '6',
480+
));
481+
}
482+
483+
/**
484+
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException
485+
*/
486+
public function testReverseTransformWithStringMonth()
487+
{
488+
$transformer = new DateTimeToArrayTransformer();
489+
$transformer->reverseTransform(array(
490+
'year' => '2010',
491+
'month' => 'bazinga',
492+
'day' => '31',
493+
'hour' => '4',
494+
'minute' => '5',
495+
'second' => '6',
496+
));
497+
}
498+
499+
/**
500+
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException
501+
*/
502+
public function testReverseTransformWithStringYear()
503+
{
504+
$transformer = new DateTimeToArrayTransformer();
505+
$transformer->reverseTransform(array(
506+
'year' => 'bazinga',
507+
'month' => '2',
508+
'day' => '31',
509+
'hour' => '4',
510+
'minute' => '5',
511+
'second' => '6',
512+
));
513+
}
466514
}

0 commit comments

Comments
 (0)