Skip to content

Commit

Permalink
[Form] Fixed display of DateTimeType and TimeType when displayed as "…
Browse files Browse the repository at this point in the history
…single_text" and "with_seconds" is false
  • Loading branch information
webmozart committed Apr 10, 2012
1 parent 07e261e commit 004c873
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-2.1.md
Expand Up @@ -263,6 +263,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
in class Form now throw an exception if the form is already bound
* fields of constrained classes without a NotBlank or NotNull constraint are
set to not required now, as stated in the docs
* fixed TimeType and DateTimeType to not display seconds when "widget" is
"single_text" unless "with_seconds" is set to true

### HttpFoundation

Expand Down
Expand Up @@ -32,7 +32,7 @@ public function buildForm(FormBuilder $builder, array $options)
$parts = array('year', 'month', 'day', 'hour', 'minute');
$timeParts = array('hour', 'minute');

$format = 'Y-m-d H:i:00';
$format = 'Y-m-d H:i';
if ($options['with_seconds']) {
$format = 'Y-m-d H:i:s';

Expand Down Expand Up @@ -102,7 +102,7 @@ public function buildForm(FormBuilder $builder, array $options)

if ('string' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], $format)
new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'])
));
} elseif ('timestamp' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer(
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Expand Up @@ -29,7 +29,7 @@ class TimeType extends AbstractType
public function buildForm(FormBuilder $builder, array $options)
{
$parts = array('hour', 'minute');
$format = 'H:i:00';
$format = 'H:i';
if ($options['with_seconds']) {
$format = 'H:i:s';
$parts[] = 'second';
Expand Down Expand Up @@ -108,7 +108,7 @@ public function buildForm(FormBuilder $builder, array $options)

if ('string' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer(
new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], $format)
new DateTimeToStringTransformer($options['data_timezone'], $options['data_timezone'], 'H:i:s')
));
} elseif ('timestamp' === $options['input']) {
$builder->appendNormTransformer(new ReversedTransformer(
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Expand Up @@ -950,7 +950,7 @@ public function testDateTimeSingleText()
[@type="text"]
[@id="name_time"]
[@name="name[time]"]
[@value="04:05:00"]
[@value="04:05"]
]
'
);
Expand All @@ -967,7 +967,7 @@ public function testDateTimeWithWidgetSingleText()
'/input
[@type="text"]
[@name="name"]
[@value="2011-02-03 04:05:00"]
[@value="2011-02-03 04:05"]
'
);
}
Expand All @@ -985,7 +985,7 @@ public function testDateTimeWithWidgetSingleTextIgnoreDateAndTimeWidgets()
'/input
[@type="text"]
[@name="name"]
[@value="2011-02-03 04:05:00"]
[@value="2011-02-03 04:05"]
'
);
}
Expand Down Expand Up @@ -1583,7 +1583,7 @@ public function testTimeSingleText()
'/input
[@type="text"]
[@name="name"]
[@value="04:05:00"]
[@value="04:05"]
'
);
}
Expand Down
Expand Up @@ -173,7 +173,7 @@ public function testSubmit_differentTimezonesDateTime()
$outputTime->setTimezone(new \DateTimeZone('America/New_York'));

$this->assertDateTimeEquals($outputTime, $form->getData());
$this->assertEquals('2010-06-02 03:04:00', $form->getClientData());
$this->assertEquals('2010-06-02 03:04', $form->getClientData());
}

public function testSubmit_stringSingleText()
Expand All @@ -188,7 +188,23 @@ public function testSubmit_stringSingleText()
$form->bind('2010-06-02 03:04:05');

$this->assertEquals('2010-06-02 03:04:00', $form->getData());
$this->assertEquals('2010-06-02 03:04:00', $form->getClientData());
$this->assertEquals('2010-06-02 03:04', $form->getClientData());
}

public function testSubmit_stringSingleText_withSeconds()
{
$form = $this->factory->create('datetime', null, array(
'data_timezone' => 'UTC',
'user_timezone' => 'UTC',
'input' => 'string',
'widget' => 'single_text',
'with_seconds' => true,
));

$form->bind('2010-06-02 03:04:05');

$this->assertEquals('2010-06-02 03:04:05', $form->getData());
$this->assertEquals('2010-06-02 03:04:05', $form->getClientData());
}

public function testSubmit_differentPattern()
Expand Down
Expand Up @@ -107,7 +107,7 @@ public function testSubmit_datetimeSingleText()
$form->bind('03:04:05');

$this->assertEquals(new \DateTime('03:04:00 UTC'), $form->getData());
$this->assertEquals('03:04:00', $form->getClientData());
$this->assertEquals('03:04', $form->getClientData());
}

public function testSubmit_arraySingleText()
Expand All @@ -127,7 +127,7 @@ public function testSubmit_arraySingleText()
$form->bind('03:04');

$this->assertEquals($data, $form->getData());
$this->assertEquals('03:04:00', $form->getClientData());
$this->assertEquals('03:04', $form->getClientData());
}

public function testSubmit_arraySingleTextWithSeconds()
Expand Down Expand Up @@ -164,7 +164,7 @@ public function testSubmit_stringSingleText()
$form->bind('03:04:05');

$this->assertEquals('03:04:00', $form->getData());
$this->assertEquals('03:04:00', $form->getClientData());
$this->assertEquals('03:04', $form->getClientData());
}

public function testSetData_withSeconds()
Expand Down

0 comments on commit 004c873

Please sign in to comment.