Skip to content
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

[Form] DateTimeType remove seconds with HTML5 #48076

Open
ivoba opened this issue Nov 2, 2022 · 4 comments
Open

[Form] DateTimeType remove seconds with HTML5 #48076

ivoba opened this issue Nov 2, 2022 · 4 comments

Comments

@ivoba
Copy link
Contributor

ivoba commented Nov 2, 2022

Description

When passing a DateTime object to DateTimeType we can not remove the seconds from the HTML5 datepicker.

There is currently no official way to disable seconds in a HTML5 dateTime input, but by not passing the seconds the seconds picker is omitted.

However we cannot set the time format in DateTimeToHtml5LocalDateTimeTransformer.
Also no format option can be used together with html5.

->add('date', null, ['label' => 'date', 'widget' => 'single_text', 'format' => 'Y-m-d H:i'])

results in

Cannot use the "format" option of "Symfony\Component\Form\Extension\Core\Type\DateTimeType" when the "html5" option is enabled.

So i would suggest to either allow format option or add a dedicated without_seconds option for HTML5 and single_text.

Example

No response

@fancyweb
Copy link
Contributor

fancyweb commented Nov 2, 2022

From what I understood, in theory, the format without the seconds is invalid because the value should respect the partial-time format from the RFC 3339. However, it seems that the browsers accept a value without the time-second part and don't display the "seconds" part this case.

We could support it too but do we want to? 🤔 That would certainly be a new behavior so a new feature.

@ivoba
Copy link
Contributor Author

ivoba commented Nov 3, 2022

Curently i solved it with a DataTransformer DateTimeNoSecondsTransformer.php

$builder->get('date')->addModelTransformer(new DateTimeNoSecondsTransformer());

Would be more convenient to have this integrated in symfony forms though.

@fancyweb
Copy link
Contributor

fancyweb commented Nov 3, 2022

The with_seconds option defaults to false. It is honored when the form doesn't have data (and when the browser doesn't display the seconds). But it's not honored when the form has data.

Since it's "working" when the form doesn't have data, I guess it could actually be treated as a bug. Especially since DateTimeToHtml5LocalDateTimeTransformer::reverseTransform() already handle strings without seconds. But I'm pretty sure there will be complaints. Our own tests rely on the current behavior.

Friendly ping to form experts @xabbuh @yceruto 😄 WDYT we should do?

@fancyweb fancyweb added Bug and removed Feature labels Nov 3, 2022
@xabbuh
Copy link
Member

xabbuh commented Jan 20, 2023

Probably this is something nobody ever thought about. IIRC. the handling in reverseTransform() was added as some browsers didn't not always send the seconds. However not displaying seconds when with_seconds is false would be a BC break as that option is false by default).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants