Check for hour, minute & second validity
This commit is contained in:
parent
04471c9916
commit
d0555f4498
@ -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');
|
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');
|
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');
|
throw new TransformationFailedException('This year is invalid');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,6 +160,18 @@ class DateTimeToArrayTransformer extends BaseDateTimeTransformer
|
|||||||
throw new TransformationFailedException('This is an invalid date');
|
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 {
|
try {
|
||||||
$dateTime = new \DateTime(sprintf(
|
$dateTime = new \DateTime(sprintf(
|
||||||
'%s-%s-%s %s:%s:%s %s',
|
'%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(
|
$form = $this->factory->createNamed('name', 'datetime', $data, array(
|
||||||
'input' => 'array',
|
'input' => 'array',
|
||||||
'empty_value' => array('hour' => 'Change&Me', 'minute' => 'Change&Me'),
|
|
||||||
'required' => false,
|
'required' => false,
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -930,10 +929,10 @@ abstract class AbstractLayoutTest extends \Symfony\Component\Form\Test\FormInteg
|
|||||||
[
|
[
|
||||||
./select
|
./select
|
||||||
[@id="name_time_hour"]
|
[@id="name_time_hour"]
|
||||||
[./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Change&Me[/trans]"]]
|
[./option[@value="4"][@selected="selected"]]
|
||||||
/following-sibling::select
|
/following-sibling::select
|
||||||
[@id="name_time_minute"]
|
[@id="name_time_minute"]
|
||||||
[./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Change&Me[/trans]"]]
|
[./option[@value="5"][@selected="selected"]]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
[count(.//select)=5]
|
[count(.//select)=5]
|
||||||
|
@ -509,4 +509,52 @@ class DateTimeToArrayTransformerTest extends DateTimeTestCase
|
|||||||
'second' => '6',
|
'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