DateIntervalType 字段
此字段允许用户选择一个时间间隔。例如,如果您想允许用户选择他们接收状态邮件的频率,他们可以使用此字段来选择诸如每 “10 分钟” 或 “3 天” 的间隔。
该字段可以用多种不同的方式呈现(参见 widget),并且可以配置为为您提供 DateInterval
对象,一个 ISO 8601 持续时间字符串(例如 P1DT12H
)或一个数组(参见 input)。
底层数据类型 | 可以是 DateInterval 、字符串或数组(参见 input 选项) |
呈现为 | 单个文本框、多个文本框或选择字段 - 参见 widget 选项 |
默认无效消息 | 请选择有效的时间间隔。 |
父类型 | FormType |
类 | DateIntervalType |
提示
此表单类型定义和继承的完整选项列表可通过在您的应用程序中运行此命令获得
1 2
# replace 'FooType' by the class name of your form type
$ php bin/console debug:form FooType
基本用法
此字段类型是高度可配置的。最重要的选项是 input 和 widget。
您可以配置很多不同的选项,包括精确地显示哪些范围选项(例如,不显示 “months”,但显示 “days”)
1 2 3 4 5 6 7 8 9 10
$builder->add('remindEvery', DateIntervalType::class, [
'widget' => 'integer', // render a text field for each part
// 'input' => 'string', // if you want the field to return a ISO 8601 string back to you
// customize which text boxes are shown
'with_years' => false,
'with_months' => false,
'with_days' => true,
'with_hours' => true,
]);
字段选项
days
类型: array
默认值: 0 到 31
可用于 days 字段类型的可用天数列表。此选项仅在 widget
选项设置为 choice
时相关
1 2 3 4 5
// values displayed to users range from 0 to 30 (both inclusive)
'days' => range(1, 31),
// values displayed to users range from 1 to 31 (both inclusive)
'days' => array_combine(range(1, 31), range(1, 31)),
placeholder
类型: string
或 array
如果您的 widget 选项设置为 choice
,则此字段将表示为一系列 select
框。placeholder
选项可用于向每个选择框的顶部添加一个 “空白” 条目
1 2 3
$builder->add('remindEvery', DateIntervalType::class, [
'placeholder' => '',
]);
或者,您可以指定一个字符串以显示为 “空白” 值
1 2 3
$builder->add('remindEvery', DateIntervalType::class, [
'placeholder' => ['years' => 'Years', 'months' => 'Months', 'days' => 'Days'],
]);
hours
类型: array
默认值: 0 到 24
可用于 hours 字段类型的可用小时数列表。此选项仅在 widget
选项设置为 choice
时相关
1 2 3 4 5
// values displayed to users range from 0 to 23 (both inclusive)
'hours' => range(1, 24),
// values displayed to users range from 1 to 24 (both inclusive)
'hours' => array_combine(range(1, 24), range(1, 24)),
input
类型: string
默认值: dateinterval
输入数据的格式 - 即间隔以何种格式存储在您的底层对象上。有效值包括
string
(使用 ISO 8601 标准格式化的字符串,例如P7Y6M5DT12H15M30S
)dateinterval
(DateInterval
对象)array
(例如['days' => '1', 'hours' => '12',]
)
从表单返回的值也将被规范化回此格式。
labels
类型: array
默认值: (见下文)
为此类型的每个元素显示的标签。默认值为 null
,因此它们显示子名称的 “人性化版本”(Invert
, Years
等)
1 2 3 4 5 6 7 8 9 10
'labels' => [
'invert' => null,
'years' => null,
'months' => null,
'weeks' => null,
'days' => null,
'hours' => null,
'minutes' => null,
'seconds' => null,
]
minutes
类型: array
默认值: 0 到 60
可用于 minutes 字段类型的可用分钟数列表。此选项仅在 widget
选项设置为 choice
时相关
1 2 3 4 5
// values displayed to users range from 0 to 59 (both inclusive)
'minutes' => range(1, 60),
// values displayed to users range from 1 to 60 (both inclusive)
'minutes' => array_combine(range(1, 60), range(1, 60)),
months
类型: array
默认值: 0 到 12
可用于 months 字段类型的可用月份列表。此选项仅在 widget
选项设置为 choice
时相关
1 2 3 4 5
// values displayed to users range from 0 to 11 (both inclusive)
'months' => range(1, 12),
// values displayed to users range from 1 to 12 (both inclusive)
'months' => array_combine(range(1, 12), range(1, 12)),
seconds
类型: array
默认值: 0 到 60
可用于 seconds 字段类型的可用秒数列表。此选项仅在 widget
选项设置为 choice
时相关
1 2 3 4 5
// values displayed to users range from 0 to 59 (both inclusive)
'seconds' => range(1, 60),
// values displayed to users range from 1 to 60 (both inclusive)
'seconds' => array_combine(range(1, 60), range(1, 60)),
weeks
类型: array
默认值: 0 到 52
可用于 weeks 字段类型的可用周数列表。此选项仅在 widget
选项设置为 choice
时相关
1 2 3 4 5
// values displayed to users range from 0 to 51 (both inclusive)
'weeks' => range(1, 52),
// values displayed to users range from 1 to 52 (both inclusive)
'weeks' => array_combine(range(1, 52), range(1, 52)),
widget
类型: string
默认值: choice
此字段应呈现的基本方式。可以是以下之一
choice
:为年、月、周、日、小时、分钟和/或秒呈现一到六个选择输入,具体取决于 with_years、with_months、with_weeks、with_days、with_hours、with_minutes 和 with_seconds 选项。默认值:年、月和日三个字段。text
:为年、月、周、日、小时、分钟和/或秒呈现一到六个文本输入,具体取决于 with_years、with_months、with_weeks、with_days、with_hours、with_minutes 和 with_seconds 选项。默认值:年、月和日三个字段。integer
:为年、月、周、日、小时、分钟和/或秒呈现一到六个整数输入,具体取决于 with_years、with_months、with_weeks、with_days、with_hours、with_minutes 和 with_seconds 选项。默认值:年、月和日三个字段。single_text
:呈现一个text
类型的单个输入。用户的输入将根据PnYnMnDTnHnMnS
格式(或仅使用周时为PnW
)进行验证。
years
类型: array
默认值: 0 到 100
可用于 years 字段类型的可用年数列表。此选项仅在 widget
选项设置为 choice
时相关
1 2 3 4 5
// values displayed to users range from 0 to 99 (both inclusive)
'years' => range(1, 100),
// values displayed to users range from 1 to 100 (both inclusive)
'years' => array_combine(range(1, 100), range(1, 100)),
覆盖的选项
invalid_message
类型: string
默认值: This value is not valid
如果输入到此字段的数据没有意义(即验证失败),则会使用此验证错误消息。
例如,如果用户在 TimeType 字段中输入了无法转换为实际时间的乱码字符串,或者如果用户在数字字段中输入了字符串(例如 apple
),则可能会发生这种情况。
正常的(业务逻辑)验证(例如设置字段的最小长度)应使用带有验证规则的验证消息进行设置(参考)。
继承的选项
这些选项继承自 form 类型
attr
类型: array
默认值: []
如果您想向 HTML 字段表示形式添加额外的属性,您可以使用 attr
选项。它是一个关联数组,其中 HTML 属性作为键。当您需要为某些 widget 设置自定义类时,这可能很有用
1 2 3
$builder->add('body', TextareaType::class, [
'attr' => ['class' => 'tinymce'],
]);
另请参阅
如果您想将这些属性添加到 表单类型行 元素,请使用 row_attr
选项。
data
类型: mixed
默认值: 默认为底层结构的字段。
当您创建表单时,每个字段最初显示表单的域数据的相应属性的值(例如,如果您将对象绑定到表单)。如果您想覆盖表单或单个字段的此初始值,则可以在 data 选项中设置它
1 2 3 4 5 6
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
// ...
$builder->add('token', HiddenType::class, [
'data' => 'abcdef',
]);
警告
data
选项始终覆盖呈现时从域数据(对象)获取的值。这意味着当表单编辑已持久化的对象时,对象值也会被覆盖,从而导致对象在表单提交时丢失其持久化的值。
help
类型: string
或 TranslatableInterface
默认值: null
允许您为表单字段定义帮助消息,默认情况下,该消息呈现在字段下方
1 2 3 4 5 6 7 8 9 10 11 12 13
use Symfony\Component\Translation\TranslatableMessage;
$builder
->add('zipCode', null, [
'help' => 'The ZIP/Postal code for your credit card\'s billing address.',
])
// ...
->add('status', null, [
'help' => new TranslatableMessage('order.status', ['%order_id%' => $order->getId()], 'store'),
])
;
help_attr
类型: array
默认值: []
设置用于显示表单字段的帮助消息的元素的 HTML 属性。它的值是一个关联数组,HTML 属性名称作为键。这些属性也可以在模板中设置
1 2 3
{{ form_help(form.name, 'Your name', {
'help_attr': {'class': 'CUSTOM_LABEL_CLASS'}
}) }}
help_html
类型: boolean
默认值: false
默认情况下,help
选项的内容在模板中呈现之前会被转义。将此选项设置为 true
以不转义它们,这在帮助包含 HTML 元素时很有用。
inherit_data
类型: boolean
默认值: false
此选项确定表单是否将从其父表单继承数据。如果您有一组在多个表单中重复的字段,这将很有用。请参阅 如何使用 "inherit_data" 减少代码重复。
警告
当字段设置了 inherit_data
选项时,它会按原样使用父表单的数据。这意味着 数据转换器 将不会应用于该字段。
invalid_message_parameters
类型: array
默认值: []
设置 invalid_message
选项时,您可能需要在字符串中包含一些变量。这可以通过向该选项添加占位符并将变量包含在此选项中来完成
1 2 3 4 5
$builder->add('someField', SomeFormType::class, [
// ...
'invalid_message' => 'You entered an invalid value, it should include %num% letters',
'invalid_message_parameters' => ['%num%' => 6],
]);
row_attr
类型: array
默认值: []
添加到用于呈现 表单类型行 的元素的 HTML 属性的关联数组
1 2 3
$builder->add('body', TextareaType::class, [
'row_attr' => ['class' => 'text-editor', 'id' => '...'],
]);
另请参阅
如果您想将这些属性添加到 表单类型 widget 元素,请使用 attr
选项。
字段变量
变量 | 类型 | 用法 |
---|---|---|
widget |
mixed |
widget 选项的值。 |
with_days |
Boolean |
with_days 选项的值。 |
with_invert |
Boolean |
with_invert 选项的值。 |
with_hours |
Boolean |
with_hours 选项的值。 |
with_minutes |
Boolean |
with_minutes 选项的值。 |
with_months |
Boolean |
with_months 选项的值。 |
with_seconds |
Boolean |
with_seconds 选项的值。 |
with_weeks |
Boolean |
with_weeks 选项的值。 |
with_years |
Boolean |
with_years 选项的值。 |