-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Description
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