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
This commit is contained in:
commit
2997baae24
@ -144,15 +144,15 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
|
||||
));
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
@ -160,6 +160,18 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
|
||||
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',
|
||||
|
@ -899,13 +899,12 @@ abstract class AbstractLayoutTest extends \Symfony\Component\Form\Test\FormInteg
|
||||
);
|
||||
}
|
||||
|
||||
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,
|
||||
));
|
||||
|
||||
@ -930,10 +929,10 @@ abstract class AbstractLayoutTest extends \Symfony\Component\Form\Test\FormInteg
|
||||
[
|
||||
./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]
|
||||
|
@ -509,4 +509,52 @@ class DateTimeToArrayTransformerTest extends DateTimeTestCase
|
||||
'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' => '',
|
||||
));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user