Symfony 2.1.如何以 12 小时 am/pm 格式显示 single_text 时间字段? [英] Symfony 2.1. How can I display a single_text time field in 12 hour am/pm format?

查看:27
本文介绍了Symfony 2.1.如何以 12 小时 am/pm 格式显示 single_text 时间字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单上有一个时间字段,小部件设置为single_text".

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

我的美国用户需要 12 小时 am/pm 格式.

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

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

虽然不是很明显,但它接受 2:15pm 的输入,但如果表单由于另一个字段上的验证错误而返回,或者稍后打开以编辑现有记录,那么它会显示 14:15,这当然是正确的但可能令人反感.

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.

如何让它显示下午 2 点 15 分?我看不到这样做的选项.

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

推荐答案

好吧,事实证明这比应该的要难.问题是 Time 表单类型不允许您指定 Date 表单类型那样的格式,并且它使用的格式是 H:i.是的,24 小时制 :-(

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 :-(

因此,唯一的解决方案是构建您自己的使用 h:i 格式的表单类型.您可以在此处找到有关如何构建自己的表单类型的说明:

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

这篇关于Symfony 2.1.如何以 12 小时 am/pm 格式显示 single_text 时间字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆