8000 [Form] Support for DatePoint fields · Issue #60698 · symfony/symfony · GitHub
[go: up one dir, main page]

8000
Skip to content
[Form] Support for DatePoint fields #60698
Closed
@Fabrn

Description

@Fabrn

Description

Hello there !

As I was pretty hyped to use DatePoint for my new entities, I made one, built a form, but encountered something wrong on form submitting : a DateTime instance was given to my DatePoint property.

I then figured out that there is no DatePointType to support converting the chosen DateTime to a DatePoint instance. In order to get this working, I had to create a very simple FormType with a ModelTransformer that would reverse transform the DateTime into a DatePoint instance. However, I think this could be integrated to Symfony, because it feels incomplete right now.

The only concern I thought about is : difference between Date and DateTime. Should we have both DatePointType and DateTimePointType ? I don't know.

Waiting to here from you guys.

Thanks !

Example

$builder->add('date', DatePointType::class);
class DatePointType extends AbstractType
{
    public function __construct(
        private readonly DatePointModelTransformer $modelTransformer,
    ) {}

    public function getParent(): string
    {
        return DateTimeType::class;
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->addModelTransformer($this->modelTransformer);
    }
}
final readonly class DatePointModelTransformer implement DataTransformerInterface
{
    public function transform(mixed $value): string
    {
        return $value;
    }

    public function reverseTransform(mixed $value): ?DatePoint
    {
        if (null === $value) {
            return null;
        }

        return DatePoint::createFromInterface($value);
    }
}

Just wrote the code on Github from memory, sorry it is incorrect

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0