8000 [Form] `TimeType` with a `reference_date`, `view_timezone` and `model_timezone` with a `widget` to `single_text` is not displaying correctly with `HTML5` enabled. · Issue #46137 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] TimeType with a reference_date, view_timezone and model_timezone with a widget to single_text is not displaying correctly with HTML5 enabled. #46137

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

Closed
RSickenberg opened this issue Apr 21, 2022 · 6 comments

Comments

@RSickenberg
Copy link
RSickenberg commented Apr 21, 2022

Symfony version(s) affected

6.0.0 - 6.0.8

Description

Hello folks!

When creating a TimeType form field with the widget as single_text and timezones the displayed value is not properly formatted for the browser.

$fakeEntity = new \stdClass();
$fakeEntity->time = new \DateTime();

// Create the form field
->add('time', TimeType::class, [
    'html5' => true,
    'widget' => 'single_text',
    'view_timezone' => 'Europe/Paris',
    'model_timezone' => 'UTC',
    'reference_date' => new \DateTime('now', new \DateTimeZone('UTC')),
])

is outputting in HTML:

<input type="time" id="form_time" name="form[time]" required="required" class="shadow-sm focus:ring-blue focus:border-blue-500 block w-full sm:text-sm border-gray-300 rounded-md" value="2022-04-21 14:49">

How to reproduce

$fakeEntity = new \stdClass();
$fakeEntity->time = new \DateTime();

// Create the form field
->add('time', TimeType::class, [
    'html5' => true,
    'widget' => 'single_text',
    'view_timezone' => 'Europe/Paris',
    'model_timezone' => 'UTC',
    'reference_date' => new \DateTime('now', new \DateTimeZone('UTC')),
])

is outputting in HTML:

<input type="time" id="form_time" name="form[time]" required="required" class="" value="2022-04-21 14:49">

Possible Solution

My guess is here: form/Extension/Core/Type/TimeType.php:84

We are dealing with the reference_date on the PRE_SUBMIT event but it's maybe too soon? The date shouldn't be displayed yet with the option HTML5 enabled.

Additional Context

image

Here a result from my project with the reproduced step.

image

And the result if I remove the date from the value field.

image

And the result if I set HTML5 to false.

Many thanks & best regards 😊

@RSickenberg RSickenberg changed the title Form TimeType with a reference_date, view_timezone and model_timezone with a widget to single_text is not displaying correct info. Form TimeType with a reference_date, view_timezone and model_timezone with a widget to single_text is not displaying correctly with HTML5 enabled. Apr 21, 2022
@RSickenberg RSickenberg changed the title Form TimeType with a reference_date, view_timezone and model_timezone with a widget to single_text is not displaying correctly with HTML5 enabled. [Form] TimeType with a reference_date, view_timezone and model_timezone with a widget to single_text is not displaying correctly with HTML5 enabled. Apr 21, 2022
@xabbuh
Copy link
Member
xabbuh commented Apr 22, 2022

Can you create a small example application that allows to reproduce your issue?

@RSickenberg
Copy link
Author

Hello @xabbuh!

Sure thing, here for you: https://github.com/RSickenberg/Symfony_bug_46137
You just need to have to get the /demo URI.

Regards

@RSickenberg
Copy link
Author

Sorry for the spam @xabbuh, did you get my notification?

@xabbuh
Copy link
Member
xabbuh commented Apr 26, 2022

Yes, but I didn't have the time yet to look into this issue.

@xabbuh xabbuh added the Form label Apr 26, 2022
@RSickenberg
Copy link
Author

Sure, no problem :)

@xabbuh
Copy link
Member
xabbuh commented Apr 29, 2022

Can you confirm that #46216 fixes it for you?

nicolas-grekas added a commit that referenced this issue Apr 30, 2022
…fferent timezones (xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Form] fix populating single widget time view data with different timezones

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #46137
| License       | MIT
| Doc PR        |

Commits
-------

430f7c4 [Form] fix populating single widget time view data with different timezones
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
0