8000 [Form][DateTimeToArrayTransformer] Check for hour, minute & second validity by egeloen · Pull Request #9441 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form][DateTimeToArrayTransformer] Check for hour, minute & second validity #9441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,34 @@ public function reverseTransform($value)
));
}

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

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

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

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

if (isset($value['hour']) && !ctype_digit((string) $value['hour'])) {
throw new TransformationFailedException('This hour is invalid');
}

if (isset($value['minute']) && !ctype_digit((string) $value['minute'])) {
throw new TransformationFailedException('This minute is invalid');
}

if (isset($value['second']) && !ctype_digit((string) $value['second'])) {
throw new TransformationFailedException('This second is invalid');
}

try {
$dateTime = new \DateTime(sprintf(
'%s-%s-%s %s:%s:%s %s',
Expand Down
9 changes: 4 additions & 5 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -900,13 +900,12 @@ public function testDateTimeWithEmptyValueGlobal()
);
}

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

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

Expand All @@ -931,10 +930,10 @@ public function testDateTimeWithEmptyValueOnTime()
[
./select
[@id="name_time_hour"]
[./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Change&Me[/trans]"]]
[./option[@value="4"][@selected="selected"]]
/following-sibling::select
[@id="name_time_minute"]
[./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Change&Me[/trans]"]]
[./option[@value="5"][@selected="selected"]]
]
]
[count(.//select)=5]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,4 +509,52 @@ public function testReverseTransformWithStringYear()
'second' => '6',
));
}

/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testReverseTransformWithEmptyStringHour()
{
$transformer = new DateTimeToArrayTransformer();
$transformer->reverseTransform(array(
'year' => '2010',
'month' => '2',
'day' => '31',
'hour' => '',
'minute' => '5',
'second' => '6',
));
}

/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testReverseTransformWithEmptyStringMinute()
{
$transformer = new DateTimeToArrayTransformer();
$transformer->reverseTransform(array(
'year' => '2010',
'month' => '2',
'day' => '31',
'hour' => '4',
'minute' => '',
'second' => '6',
));
}

/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testReverseTransformWithEmptyStringSecond()
{
$transformer = new DateTimeToArrayTransformer();
$transformer->reverseTransform(array(
'year' => '2010',
'month' => '2',
'day' => '31',
'hour' => '4',
'minute' => '5',
'second' => '',
));
}
}
0