8000 bug #9441 [Form][DateTimeToArrayTransformer] Check for hour, minute &… · symfony/symfony@2997baa · GitHub
[go: up one dir, main page]

Skip to content

Commit 2997baa

Browse files
committed
bug #9441 [Form][DateTimeToArrayTransformer] Check for hour, minute & second validity (egeloen)
This PR was submitted for the 2.3-dev branch but it was merged into the 2.3 branch instead (closes #9441). Discussion ---------- [Form][DateTimeToArrayTransformer] Check for hour, minute & second validity | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #9440 | License | MIT | Doc PR | - This PR checks if hour, minute & second values are valid in the datetime to array transformer (values must be integer if they exist). Commits ------- 1543653 [Form][DateTimeToArrayTransformer] Check for hour, minute & second validity
2 parents 04471c9 + d0555f4 commit 2997baa

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,34 @@ public function reverseTransform($value)
144144
));
145145
}
146146

147-
if (isset($value['month']) && !ctype_digit($value['month']) && !is_int($value['month'])) {
147+
if (isset($value['month']) && !ctype_digit((string) $value['month'])) {
148148
throw new TransformationFailedException('This month is invalid');
149149
}
150150

151-
if (isset($value['day']) && !ctype_digit($value['day']) && !is_int($value['day'])) {
151+
if (isset($value['day']) && !ctype_digit((string) $value['day'])) {
152152
throw new TransformationFailedException('This day is invalid');
153153
}
154154

155-
if (isset($value['year']) && !ctype_digit($value['year']) && !is_int($value['year'])) {
155+
if (isset($value['year']) && !ctype_digit((string) $value['year'])) {
156156
throw new TransformationFailedException('This year is invalid');
157157
}
158158

159159
if (!empty($value['month']) && !empty($value['day']) && !empty($value['year']) && false === checkdate($value['month'], $value['day'], $value['year'])) {
160160
throw new TransformationFailedException('This is an invalid date');
161161
}
162162

163+
if (isset($value['hour']) && !ctype_digit((string) $value['hour'])) {
164+
throw new TransformationFailedException('This hour is invalid');
165+
}
166+
167+
if (isset($value['minute']) && !ctype_digit((string) $value['minute'])) {
168+
throw new TransformationFailedException('This minute is invalid');
169+
}
170+
171+
if (isset($value['second']) && !ctype_digit((string) $value['second'])) {
172+
throw new TransformationFailedException('This second is invalid');
173+
}
174+
163175
try {
164176
$dateTime = new \DateTime(sprintf(
165177
'%s-%s-%s %s:%s:%s %s',

src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -899,13 +899,12 @@ public function testDateTimeWithEmptyValueGlobal()
899899
);
900900
}
901901

902-
public function testDateTimeWithEmptyValueOnTime()
902+
public function testDateTimeWithHourAndMinute()
903903
{
904-
$data = array('year' => '2011', 'month' => '2', 'day' => '3', 'hour' => '', 'minute' => '');
904+
$data = array('year' => '2011', 'month' => '2', 'day' => '3', 'hour' => '4', 'minute' => '5');
905905

906906
$form = $this->factory->createNamed('name', 'datetime', $data, array(
907907
'input' => 'array',
908-
'empty_value' => array('hour' => 'Change&Me', 'minute' => 'Change&Me'),
909908
'required' => false,
910909
));
911910

@@ -930,10 +929,10 @@ public function testDateTimeWithEmptyValueOnTime()
930929
[
931930
./select
932931
[@id="name_time_hour"]
933-
[./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Change&Me[/trans]"]]
932+
[./option[@value="4"][@selected="selected"]]
934933
/following-sibling::select
935934
[@id="name_time_minute"]
936-
[./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Change&Me[/trans]"]]
935+
[./option[@value="5"][@selected="selected"]]
937936
]
938937
]
939938
[count(.//select)=5]

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,4 +509,52 @@ public function testReverseTransformWithStringYear()
509509
'second' => '6',
510510
));
511511
}
512+
513+
/**
514+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
515+
*/
516+
public function testReverseTransformWithEmptyStringHour()
517+
{
518+
$transformer = new DateTimeToArrayTransformer();
519+
$transformer->reverseTransform(array(
520+
'year' => '2010',
521+
'month' => '2',
522+
'day' => '31',
523+
'hour' => '',
524+
'minute' => '5',
525+
'second' => '6',
526+
));
527+
}
528+
529+
/**
530+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
531+
*/
532+
public function testReverseTransformWithEmptyStringMinute()
533+
{
534+
$transformer = new DateTimeToArrayTransformer();
535+
$transformer->reverseTransform(array(
536+
'year' => '2010',
537+
'month' => '2',
538+
'day' => '31',
539+
'hour' => '4',
540+
'minute' => '',
541+
'second' => '6',
542+
));
543+
}
544+
545+
/**
546+
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
547+
*/
548+
public function testReverseTransformWithEmptyStringSecond()
549+
{
550+
$transformer = new DateTimeToArrayTransformer();
551+
$transformer->reverseTransform(array(
552+
'year' => '2010',
553+
'month' => '2',
554+
'day' => '31',
555+
'hour' => '4',
556+
'minute' => '5',
557+
'second' => '',
558+
));
559+
}
512560
}

0 commit comments

Comments
 (0)
0