Skip to content

Commit

Permalink
[Form] Fixed DateType/TimeType that were broken since 849fb29 and 1c4…
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Apr 7, 2015
1 parent 9ca8709 commit 060d0f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 72 deletions.
12 changes: 6 additions & 6 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Expand Up @@ -57,8 +57,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)

if ('single_text' === $options['widget']) {
$builder->addViewTransformer(new DateTimeToLocalizedStringTransformer(
null,
null,
$options['model_timezone'],
$options['view_timezone'],
$dateFormat,
$timeFormat,
$calendar,
Expand Down Expand Up @@ -105,23 +105,23 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('month', $options['widget'], $monthOptions)
->add('day', $options['widget'], $dayOptions)
->addViewTransformer(new DateTimeToArrayTransformer(
null, null, array('year', 'month', 'day')
$options['model_timezone'], $options['view_timezone'], array('year', 'month', 'day')
))
->setAttribute('formatter', $formatter)
;
}

if ('string' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
new DateTimeToStringTransformer(null, null, 'Y-m-d')
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], 'Y-m-d')
));
} elseif ('timestamp' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
new DateTimeToTimestampTransformer(null, null)
new DateTimeToTimestampTransformer($options['model_timezone'], $options['model_timezone'])
));
} elseif ('array' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
new DateTimeToArrayTransformer(null, null, array('year', 'month', 'day'))
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], array('year', 'month', 'day'))
));
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Expand Up @@ -48,7 +48,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}

if ('single_text' === $options['widget']) {
$builder->addViewTransformer(new DateTimeToStringTransformer(null, null, $format));
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
} else {
$hourOptions = $minuteOptions = $secondOptions = array(
'error_bubbling' => true,
Expand Down Expand Up @@ -109,20 +109,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->add('second', $options['widget'], $secondOptions);
}

$builder->addViewTransformer(new DateTimeToArrayTransformer(null, null, $parts, 'text' === $options['widget']));
$builder->addViewTransformer(new DateTimeToArrayTransformer($options['model_timezone'], $options['view_timezone'], $parts, 'text' === $options['widget']));
}

if ('string' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
new DateTimeToStringTransformer(null, null, 'H:i:s')
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], 'H:i:s')
));
} elseif ('timestamp' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
new DateTimeToTimestampTransformer(null, null)
new DateTimeToTimestampTransformer($options['model_timezone'], $options['model_timezone'])
));
} elseif ('array' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
new DateTimeToArrayTransformer(null, null, $parts)
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts)
));
}
}
Expand Down
Expand Up @@ -380,93 +380,43 @@ public function testThrowExceptionIfDaysIsInvalid()
));
}

public function testSetDataWithDifferentNegativeUTCTimezoneDateTime()
public function testSetDataWithNegativeTimezoneOffsetStringInput()
{
$form = $this->factory->create('date', null, array(
'format' => \IntlDateFormatter::MEDIUM,
'model_timezone' => 'America/New_York',
'view_timezone' => 'Pacific/Tahiti',
'model_timezone' => 'UTC',
'view_timezone' => 'America/New_York',
'input' => 'string',
'widget' => 'single_text',
));

$form->setData('2010-06-02');

// 2010-06-02 00:00:00 UTC
// 2010-06-01 20:00:00 UTC-4
$this->assertEquals('01.06.2010', $form->getViewData());
}

public function testSetDataWithDifferentTimezonesDateTime()
public function testSetDataWithNegativeTimezoneOffsetDateTimeInput()
{
$form = $this->factory->create('date', null, array(
'format' => \IntlDateFormatter::MEDIUM,
'model_timezone' => 'America/New_York',
'view_timezone' => 'Pacific/Tahiti',
'model_timezone' => 'UTC',
'view_timezone' => 'America/New_York',
'input' => 'datetime',
'widget' => 'single_text',
));

$dateTime = new \DateTime('2010-06-02 America/New_York');
$dateTime = new \DateTime('2010-06-02 UTC');

$form->setData($dateTime);

// 2010-06-02 00:00:00 UTC
// 2010-06-01 20:00:00 UTC-4
$this->assertDateTimeEquals($dateTime, $form->getData());
$this->assertEquals('01.06.2010', $form->getViewData());
}

public function testSetDataWithDifferentPositiveUTCTimezoneDateTime()
{
date_default_timezone_set('Pacific/Tahiti');

$form = $this->factory->create('date', null, array(
'format' => \IntlDateFormatter::MEDIUM,
'input' => 'datetime',
'widget' => 'single_text',
));

$dateTime = new \DateTime('2010-06-02 Australia/Melbourne');

$form->setData($dateTime);

$this->assertDateTimeEquals($dateTime, $form->getData());
$this->assertEquals('02.06.2010', $form->getViewData());
}

public function testSetDataWithSamePositiveUTCTimezoneDateTime()
{
date_default_timezone_set('Australia/Melbourne');

$form = $this->factory->create('date', null, array(
'format' => \IntlDateFormatter::MEDIUM,
'input' => 'datetime',
'widget' => 'single_text',
));

$dateTime = new \DateTime('2010-06-02 Australia/Melbourne');

$form->setData($dateTime);

$this->assertDateTimeEquals($dateTime, $form->getData());
$this->assertEquals('02.06.2010', $form->getViewData());
}

public function testSetDataWithSameNegativeUTCTimezoneDateTime()
{
date_default_timezone_set('America/New_York');

$form = $this->factory->create('date', null, array(
'format' => \IntlDateFormatter::MEDIUM,
'input' => 'datetime',
'widget' => 'single_text',
));

$dateTime = new \DateTime('2010-06-02 America/New_York');

$form->setData($dateTime);

$this->assertDateTimeEquals($dateTime, $form->getData());
$this->assertEquals('02.06.2010', $form->getViewData());
}

public function testYearsOption()
{
$form = $this->factory->create('date', null, array(
Expand Down

0 comments on commit 060d0f8

Please sign in to comment.