跳到内容

EasyAdmin 日期字段

编辑此页

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

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

Default style of EasyAdmin date field

基本信息

  • PHP 类: EasyCorp\Bundle\EasyAdminBundle\Field\DateField
  • 用于存储此值的 Doctrine DBAL 类型: datedate_immutable
  • 用于渲染字段的 Symfony 表单类型: DateType
  • 渲染为:

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

选项

renderAsChoice

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

如果您希望将日期显示为 3 个单独的 <select> 元素,以分别选择日、月和年,请使用此选项

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

注意

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

renderAsNativeWidget

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

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

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

注意

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

renderAsText

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

如果您希望将日期显示为单个 <input type="text"> 元素,请使用此选项

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

注意

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

setFormat

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

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

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

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

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

1
2
yield DateField::new('...')->setFormat('yyyy.MM.dd G');
yield DateField::new('...')->setFormat('EEE, MMM d, \'\'yy');

setTimezone

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

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