Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Form] Add options with_minutes to DateTimeType & TimeType

  • Loading branch information...
commit 94f78f4d04866520d0cbc05c8f67630caadfcae0 1 parent 9ba5ed8
@egeloen egeloen authored
View
11 Extension/Core/Type/DateTimeType.php
@@ -69,9 +69,14 @@ class DateTimeType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
- $parts = array('year', 'month', 'day', 'hour', 'minute');
+ $parts = array('year', 'month', 'day', 'hour');
$dateParts = array('year', 'month', 'day');
- $timeParts = array('hour', 'minute');
+ $timeParts = array('hour');
+
+ if ($options['with_minutes']) {
+ $parts[] = 'minute';
+ $timeParts[] = 'minute';
+ }
if ($options['with_seconds']) {
$parts[] = 'second';
@@ -118,6 +123,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'hours',
'minutes',
'seconds',
+ 'with_minutes',
'with_seconds',
'empty_value',
'required',
@@ -223,6 +229,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'widget' => null,
'date_widget' => $dateWidget,
'time_widget' => $timeWidget,
+ 'with_minutes' => true,
'with_seconds' => false,
// Don't modify \DateTime classes by reference, we treat
// them like immutable value objects
View
49 Extension/Core/Type/TimeType.php
@@ -15,6 +15,7 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\ReversedTransformer;
+use Symfony\Component\Form\Exception\InvalidConfigurationException;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer;
@@ -29,10 +30,20 @@ class TimeType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
- $parts = array('hour', 'minute');
- $format = 'H:i';
+ $parts = array('hour');
+ $format = 'H';
+
+ if ($options['with_seconds'] && !$options['with_minutes']) {
+ throw new InvalidConfigurationException('You can not disable minutes if you have enabled seconds.');
+ }
+
+ if ($options['with_minutes']) {
+ $format .= ':i';
+ $parts[] = 'minute';
+ }
+
if ($options['with_seconds']) {
- $format = 'H:i:s';
+ $format .= ':s';
$parts[] = 'second';
}
@@ -49,15 +60,19 @@ public function buildForm(FormBuilderInterface $builder, array $options)
foreach ($options['hours'] as $hour) {
$hours[$hour] = str_pad($hour, 2, '0', STR_PAD_LEFT);
}
- foreach ($options['minutes'] as $minute) {
- $minutes[$minute] = str_pad($minute, 2, '0', STR_PAD_LEFT);
- }
// Only pass a subset of the options to children
$hourOptions['choices'] = $hours;
$hourOptions['empty_value'] = $options['empty_value']['hour'];
- $minuteOptions['choices'] = $minutes;
- $minuteOptions['empty_value'] = $options['empty_value']['minute'];
+
+ if ($options['with_minutes']) {
+ foreach ($options['minutes'] as $minute) {
+ $minutes[$minute] = str_pad($minute, 2, '0', STR_PAD_LEFT);
+ }
+
+ $minuteOptions['choices'] = $minutes;
+ $minuteOptions['empty_value'] = $options['empty_value']['minute'];
+ }
if ($options['with_seconds']) {
$seconds = array();
@@ -72,17 +87,23 @@ public function buildForm(FormBuilderInterface $builder, array $options)
// Append generic carry-along options
foreach (array('required', 'translation_domain') as $passOpt) {
- $hourOptions[$passOpt] = $minuteOptions[$passOpt] = $options[$passOpt];
+ $hourOptions[$passOpt] = $options[$passOpt];
+
+ if ($options['with_minutes']) {
+ $minuteOptions[$passOpt] = $options[$passOpt];
+ }
+
if ($options['with_seconds']) {
$secondOptions[$passOpt] = $options[$passOpt];
}
}
}
- $builder
- ->add('hour', $options['widget'], $hourOptions)
- ->add('minute', $options['widget'], $minuteOptions)
- ;
+ $builder->add('hour', $options['widget'], $hourOptions);
+
+ if ($options['with_minutes']) {
+ $builder->add('minute', $options['widget'], $minuteOptions);
+ }
if ($options['with_seconds']) {
$builder->add('second', $options['widget'], $secondOptions);
@@ -113,6 +134,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars = array_replace($view->vars, array(
'widget' => $options['widget'],
+ 'with_minutes' => $options['with_minutes'],
'with_seconds' => $options['with_seconds'],
));
@@ -167,6 +189,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'seconds' => range(0, 59),
'widget' => 'choice',
'input' => 'datetime',
+ 'with_minutes' => true,
'with_seconds' => false,
'model_timezone' => $modelTimezone,
'view_timezone' => $viewTimezone,
View
29 Tests/Extension/Core/Type/DateTimeTypeTest.php
@@ -94,6 +94,35 @@ public function testSubmit_timestamp()
$this->assertEquals($dateTime->format('U'), $form->getData());
}
+ public function testSubmit_withoutMinutes()
+ {
+ $form = $this->factory->create('datetime', null, array(
+ 'data_timezone' => 'UTC',
+ 'user_timezone' => 'UTC',
+ 'date_widget' => 'choice',
+ 'time_widget' => 'choice',
+ 'input' => 'datetime',
+ 'with_minutes' => false,
+ ));
+
+ $form->setData(new \DateTime('2010-06-02 03:04:05 UTC'));
+
+ $input = array(
+ 'date' => array(
+ 'day' => '2',
+ 'month' => '6',
+ 'year' => '2010',
+ ),
+ 'time' => array(
+ 'hour' => '3',
+ ),
+ );
+
+ $form->bind($input);
+
+ $this->assertDateTimeEquals(new \DateTime('2010-06-02 03:00:00 UTC'), $form->getData());
+ }
+
public function testSubmit_withSeconds()
{
$form = $this->factory->create('datetime', null, array(
View
77 Tests/Extension/Core/Type/TimeTypeTest.php
@@ -111,6 +111,22 @@ public function testSubmit_datetimeSingleText()
$this->assertEquals('03:04', $form->getViewData());
}
+ public function testSubmit_datetimeSingleTextWithoutMinutes()
+ {
+ $form = $this->factory->create('time', null, array(
+ 'data_timezone' => 'UTC',
+ 'user_timezone' => 'UTC',
+ 'input' => 'datetime',
+ 'widget' => 'single_text',
+ 'with_minutes' => false,
+ ));
+
+ $form->bind('03');
+
+ $this->assertEquals(new \DateTime('1970-01-01 03:00:00 UTC'), $form->getData());
+ $this->assertEquals('03', $form->getViewData());
+ }
+
public function testSubmit_arraySingleText()
{
$form = $this->factory->create('time', null, array(
@@ -131,6 +147,26 @@ public function testSubmit_arraySingleText()
$this->assertEquals('03:04', $form->getViewData());
}
+ public function testSubmit_arraySingleTextWithoutMinutes()
+ {
+ $form = $this->factory->create('time', null, array(
+ 'data_timezone' => 'UTC',
+ 'user_timezone' => 'UTC',
+ 'input' => 'array',
+ 'widget' => 'single_text',
+ 'with_minutes' => false,
+ ));
+
+ $data = array(
+ 'hour' => '3',
+ );
+
+ $form->bind('03');
+
+ $this->assertEquals($data, $form->getData());
+ $this->assertEquals('03', $form->getViewData());
+ }
+
public function testSubmit_arraySingleTextWithSeconds()
{
$form = $this->factory->create('time', null, array(
@@ -168,6 +204,36 @@ public function testSubmit_stringSingleText()
$this->assertEquals('03:04', $form->getViewData());
}
+ public function testSubmit_stringSingleTextWithoutMinutes()
+ {
+ $form = $this->factory->create('time', null, array(
+ 'data_timezone' => 'UTC',
+ 'user_timezone' => 'UTC',
+ 'input' => 'string',
+ 'widget' => 'single_text',
+ 'with_minutes' => false,
+ ));
+
+ $form->bind('03');
+
+ $this->assertEquals('03:00:00', $form->getData());
+ $this->assertEquals('03', $form->getViewData());
+ }
+
+ public function testSetData_withoutMinutes()
+ {
+ $form = $this->factory->create('time', null, array(
+ 'data_timezone' => 'UTC',
+ 'user_timezone' => 'UTC',
+ 'input' => 'datetime',
+ 'with_minutes' => false,
+ ));
+
+ $form->setData(new \DateTime('03:04:05 UTC'));
+
+ $this->assertEquals(array('hour' => 3), $form->getClientData());
+ }
+
public function testSetData_withSeconds()
{
$form = $this->factory->create('time', null, array(
@@ -561,4 +627,15 @@ public function testSecondErrorsBubbleUp($widget)
$this->assertSame(array(), $form['second']->getErrors());
$this->assertSame(array($error), $form->getErrors());
}
+
+ /**
+ * @expectedException Symfony\Component\Form\Exception\InvalidConfigurationException
+ */
+ public function testInitializeWithSecondsAndWithoutMinutes()
+ {
+ $this->factory->create('time', null, array(
+ 'with_minutes' => false,
+ 'with_seconds' => true,
+ ));
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.