Skip to content

[Form] Add input=date_point to DateTimeType, DateType and TimeType #60315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add support for displaying nested options in DebugCommand
* Add support for strings as data for the `MoneyType`
* Add `input=date_point` to DateTimeType, DateType and TimeType
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Add `input=date_point` to DateTimeType, DateType and TimeType
* Add `input=date_point` to `DateTimeType`, `DateType` and `TimeType`


7.2
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Extension\Core\DataTransformer;

use Symfony\Component\Clock\DatePoint;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

/**
* Transforms between a DatePoint object and a DateTime object.
*
* @implements DataTransformerInterface<DatePoint, \DateTime>
*/
final class DatePointToDateTimeTransformer implements DataTransformerInterface
{
/**
* Transforms a DatePoint into a DateTime object.
*
* @param DatePoint|null $value A DatePoint object
*
* @throws TransformationFailedException If the given value is not a DatePoint
*/
public function transform(mixed $value): ?\DateTime
{
if (null === $value) {
return null;
}

if (!$value instanceof DatePoint) {
throw new TransformationFailedException(\sprintf('Expected a "%s".', DatePoint::class));
}

return \DateTime::createFromImmutable($value);
}

/**
* Transforms a DateTime object into a DatePoint object.
*
* @param \DateTime|null $value A DateTime object
*
* @throws TransformationFailedException If the given value is not a \DateTime
*/
public function reverseTransform(mixed $value): ?DatePoint
{
if (null === $value) {
return null;
}

if (!$value instanceof \DateTime) {
throw new TransformationFailedException('Expected a \DateTime.');
}

return DatePoint::createFromMutable($value);
}
}
12 changes: 10 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Clock\DatePoint;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\DataTransformer\ArrayToPartsTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DataTransformerChain;
use Symfony\Component\Form\Extension\Core\DataTransformer\DatePointToDateTimeTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeImmutableToDateTimeTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToHtml5LocalDateTimeTransformer;
Expand Down Expand Up @@ -178,7 +180,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
;
}

if ('datetime_immutable' === $options['input']) {
if ('date_point' === $options['input']) {
if (!class_exists(DatePoint::class)) {
throw new LogicException(\sprintf('The "symfony/clock" component is required to use "%s" with option "input=date_point". Try running "composer require symfony/clock".', self::class));
}
$builder->addModelTransformer(new DatePointToDateTimeTransformer());
} elseif ('datetime_immutable' === $options['input']) {
$builder->addModelTransformer(new DateTimeImmutableToDateTimeTransformer());
} elseif ('string' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
Expand All @@ -194,7 +201,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
));
}

if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
if (\in_array($options['input'], ['datetime', 'datetime_immutable', 'date_point'], true) && null !== $options['model_timezone']) {
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
$date = $event->getData();

Expand Down Expand Up @@ -283,6 +290,7 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedValues('input', [
'datetime',
'datetime_immutable',
'date_point',
'string',
'timestamp',
'array',
Expand Down
12 changes: 10 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Clock\DatePoint;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\DataTransformer\DatePointToDateTimeTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeImmutableToDateTimeTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer;
Expand Down Expand Up @@ -156,7 +158,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
;
}

if ('datetime_immutable' === $options['input']) {
if ('date_point' === $options['input']) {
if (!class_exists(DatePoint::class)) {
throw new LogicException(\sprintf('The "symfony/clock" component is required to use "%s" with option "input=date_point". Try running "composer require symfony/clock".', self::class));
}
$builder->addModelTransformer(new DatePointToDateTimeTransformer());
} elseif ('datetime_immutable' === $options['input']) {
$builder->addModelTransformer(new DateTimeImmutableToDateTimeTransformer());
} elseif ('string' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
Expand All @@ -172,7 +179,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
));
}

if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
if (\in_array($options['input'], ['datetime', 'datetime_immutable', 'date_point'], true) && null !== $options['model_timezone']) {
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
$date = $event->getData();

Expand Down Expand Up @@ -298,6 +305,7 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedValues('input', [
'datetime',
'datetime_immutable',
'date_point',
'string',
'timestamp',
'array',
Expand Down
12 changes: 10 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Clock\DatePoint;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\InvalidConfigurationException;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\DataTransformer\DatePointToDateTimeTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeImmutableToDateTimeTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
Expand Down Expand Up @@ -190,7 +192,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->addViewTransformer(new DateTimeToArrayTransformer($options['model_timezone'], $options['view_timezone'], $parts, 'text' === $options['widget'], $options['reference_date']));
}

if ('datetime_immutable' === $options['input']) {
if ('date_point' === $options['input']) {
if (!class_exists(DatePoint::class)) {
throw new LogicException(\sprintf('The "symfony/clock" component is required to use "%s" with option "input=date_point". Try running "composer require symfony/clock".', self::class));
}
$builder->addModelTransformer(new DatePointToDateTimeTransformer());
} elseif ('datetime_immutable' === $options['input']) {
$builder->addModelTransformer(new DateTimeImmutableToDateTimeTransformer());
} elseif ('string' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
Expand All @@ -206,7 +213,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
));
}

if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
if (\in_array($options['input'], ['datetime', 'datetime_immutable', 'date_point'], true) && null !== $options['model_timezone']) {
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
$date = $event->getData();

Expand Down Expand Up @@ -354,6 +361,7 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedValues('input', [
'datetime',
'datetime_immutable',
'date_point',
'string',
'timestamp',
'array',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Clock\DatePoint;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\FormError;
Expand Down Expand Up @@ -62,6 +63,41 @@ public function testSubmitDateTime()
$this->assertEquals($dateTime, $form->getData());
}

public function testSubmitDatePoint()
{
if (!class_exists(DatePoint::class)) {
self::markTestSkipped('The DatePoint class is not available.');
}

$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'date_widget' => 'choice',
'years' => [2010],
'time_widget' => 'choice',
'input' => 'date_point',
]);

$input = [
'date' => [
'day' => '2',
'month' => '6',
'year' => '2010',
],
'time' => [
'hour' => '3',
'minute' => '4',
],
];

$form->submit($input);

$this->assertInstanceOf(DatePoint::class, $form->getData());
$datePoint = DatePoint::createFromMutable(new \DateTime('2010-06-02 03:04:00 UTC'));
$this->assertEquals($datePoint, $form->getData());
$this->assertEquals($input, $form->getViewData());
}

public function testSubmitDateTimeImmutable()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Clock\DatePoint;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\Type\DateType;
Expand Down Expand Up @@ -115,6 +116,27 @@ public function testSubmitFromSingleTextDateTime()
$this->assertEquals('02.06.2010', $form->getViewData());
}

public function testSubmitFromSingleTextDatePoint()
{
if (!class_exists(DatePoint::class)) {
self::markTestSkipped('The DatePoint class is not available.');
}

$form = $this->factory->create(static::TESTED_TYPE, null, [
'html5' => false,
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
'input' => 'date_point',
]);

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

$this->assertInstanceOf(DatePoint::class, $form->getData());
$this->assertEquals(DatePoint::createFromMutable(new \DateTime('2010-06-02 UTC')), $form->getData());
$this->assertEquals('2010-06-02', $form->getViewData());
}

public function testSubmitFromSingleTextDateTimeImmutable()
{
// we test against "de_DE", so we need the full implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Clock\DatePoint;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\Exception\InvalidConfigurationException;
use Symfony\Component\Form\Exception\LogicException;
Expand Down Expand Up @@ -45,6 +46,32 @@ public function testSubmitDateTime()
$this->assertEquals($input, $form->getViewData());
}

public function testSubmitDatePoint()
{
if (!class_exists(DatePoint::class)) {
self::markTestSkipped('The DatePoint class is not available.');
}

$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'choice',
'input' => 'date_point',
]);

$input = [
'hour' => '3',
'minute' => '4',
];

$form->submit($input);

$this->assertInstanceOf(DatePoint::class, $form->getData());
$datePoint = DatePoint::createFromMutable(new \DateTime('1970-01-01 03:04:00 UTC'));
$this->assertEquals($datePoint, $form->getData());
$this->assertEquals($input, $form->getViewData());
}

public function testSubmitDateTimeImmutable()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
Expand Down
Loading