跳到内容

EasyAdmin 时间字段

编辑此页

此字段用于表示存储 PHP DateTimeInterface 值(例如 DateTime, DateTimeImmutable 等)的值的时间部分

表单页面(编辑和新建)中,它看起来像这样

Default style of EasyAdmin time field

基本信息

  • PHP 类: EasyCorp\Bundle\EasyAdminBundle\Field\TimeField
  • Doctrine DBAL 类型 用于存储此值: timetime_immutable
  • Symfony 表单类型 用于渲染字段: TimeType
  • 渲染为:

    1
    <input type="time"> ... </select>

选项

renderAsChoice

默认情况下,在表单页面(editnew)中,该字段渲染为 HTML5 输入字段。这样做是因为现代浏览器为这些字段显示高级日期选择器,使其更易于使用。

如果您更喜欢将时间显示为 2 个单独的 <select> 元素来选择小时和分钟,请使用此选项

1
yield TimeField::new('...')->renderAsChoice();

注意

设置此选项等同于在底层 TimeType Symfony 字段中设置 widget = choicehtml5 = true 选项。

renderAsNativeWidget

默认情况下,在表单页面(editnew)中,该字段渲染为 HTML5 输入字段。这样做是因为现代浏览器为这些字段显示高级日期选择器,使其更易于使用。

此选项允许您以编程方式启用/禁用此行为(例如,基于某些表达式的结果)。将其设置为 false 等同于调用 renderAsChoice()

1
yield TimeField::new('...')->renderAsNativeWidget(false);

注意

设置此选项等同于在底层 TimeType Symfony 字段中设置 widget = single_texthtml5 = true 选项。

renderAsText

默认情况下,在表单页面(editnew)中,该字段渲染为 HTML5 输入字段。这样做是因为现代浏览器为这些字段显示高级日期选择器,使其更易于使用。

如果您更喜欢将时间显示为单个 <input type="text"> 元素,请使用此选项

1
yield TimeField::new('...')->renderAsText();

注意

设置此选项等同于在底层 TimeType Symfony 字段中设置 widget = single_texthtml5 = false 选项。

setFormat

默认情况下,在只读页面(indexdetail)中,时间以 setTimeFormat() CRUD 选项定义的格式显示。使用此选项可覆盖该默认格式

1
2
3
4
5
6
7
// these are the predefined formats: 'short', 'medium', 'long', 'full'
yield TimeField::new('...')->setFormat('long');

// predefined formats are available as constants too
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;

yield TimeField::new('...')->setFormat(DateTimeField::FORMAT_LONG);

除了预定义的格式外,您还可以通过将有效的 ICU Datetime Pattern 传递给此函数来配置您自己的格式

1
2
yield TimeField::new('...')->setFormat('HH:mm:ss zzz');
yield TimeField::new('...')->setFormat('K:mm a, z');

setTimezone

默认情况下,在只读页面(indexdetail)中,时间使用 setTimezone() CRUD 选项定义的时区显示。使用此选项可覆盖该默认时区(参数必须是任何有效的 PHP 时区 ID

1
yield TimeField::new('...')->setTimezone('America/Mexico_City');
这项工作,包括代码示例,根据 Creative Commons BY-SA 3.0 许可获得许可。
目录
    版本