object 检查器类型允许定义一个具有特定属性且可由用户编辑的对象。对象属性通过 properties 属性指定。该属性的值是一个数组,其结构与检查器属性数组相同。
上述示例创建了一个具有三个属性的对象。其中两个显示为文本字段,第三个显示为下拉菜单。
public function defineProperties()
{
return [
'address' => [
'title' => 'Address',
'type' => 'object',
'properties' => [
'streetAddress' => [
'title' => 'Street Address',
'type' => 'string'
],
'city' => [
'title' => 'City',
'type' => 'string'
],
'country' => [
'title' => 'Country',
'type' => 'dropdown',
'options' => [
'us' => 'US',
'ca' => 'Canada'
]
]
],
]
];
}生成的输出是一个对象,例如:
"address": {
"streetAddress": "321-210 Second ave",
"city": "Springfield",
"country": "us"
}以下配置值是常用且受支持的。
| Property | Description |
|---|---|
| title | title for the property. |
| description | a brief description of the property, optional. |
| properties | an array of nested property definitions. |
| default | an array of populated items by default containing keys and values. |
| ignoreIfPropertyEmpty | set to an array of values that should be excluded from the output if the value is empty. |
此类型不支持由
showExternalParam属性指定的外部参数编辑器。
对象属性可以是检查器支持的任何类型,包括其他对象。有一种方法可以完全将某个对象从检查器值中排除,如果该对象的某个字段为空。该字段通过 ignoreIfPropertyEmpty 参数标识。例如:
public function defineProperties()
{
return [
'address' => [
'title' => 'Address',
'type' => 'object',
'ignoreIfPropertyEmpty' => 'title',
'properties' => [
'streetAddress' => [
'title' => 'Street Address',
'type' => 'string'
],
'city' => [
'title' => 'City',
'type' => 'string'
]
],
]
];
}在上述示例中,如果未指定街道地址,对象("address")将从检查器输出中被完全移除。如果定义了任何在其他对象属性上的验证规则,并且所需的属性为空,那些规则将被忽略。
编辑器的 默认 值(如果指定)应该是一个对象,其属性与在 properties 配置参数中定义的相同。
public function defineProperties()
{
return [
'address' => [
'title' => 'Address',
'type' => 'object',
'properties' => [/*...*/],
'default' => [
'streetAddress' => '321-210 Second ave',
'city' => 'Springfield',
'country' => 'us'
]
]
];
}