Symfony 2.1. How can I display a single_text time field in 12 hour am/pm format?

感情迁移 提交于 2019-12-11 03:07:12

问题


I have a time field on my form with widget set to 'single_text'.

My US users will want 12 hour am/pm format.

http://symfony.com/doc/current/reference/forms/types/time.html#widget

Although it's not entirely obvious, that accepts an input of 2:15pm but if the form returns because of a validation error on another field or is later opened to edit an existing record then it displays 14:15 which of course is correct but is probably off-putting.

How do I get it to display 2:15pm? I don't see an option to do that.


回答1:


Well, this turns out to be harder than it should be. The problem is that the Time form type does not allow you to specify a format like the Date form type does and the format it uses is H:i. Yes, 24-hour format :-(

So, the only solution is to build your own form type which uses the h:i format. You can find instructions for how to build your own form type here:

http://symfony.com/doc/2.1/cookbook/form/create_custom_field_type.html

Then you can use the Time form type as the base for your form type and just change the format it uses. The Time form type can be found here:

/Symfony/Component/Form/Extension/Core/Type/TimeType.php


来源:https://stackoverflow.com/questions/12443989/symfony-2-1-how-can-i-display-a-single-text-time-field-in-12-hour-am-pm-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!