-->

Symfony 2.1. How can I display a single_text time

2019-07-19 08:40发布

问题:

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