Skip to content

[Form] Support for DatePoint fields #60698

@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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy