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属性所指定的外部参数编辑器。
对象的属性可以是 Inspector 支持的任何类型,包括其他对象。如果对象的某个字段为空,则有一种方法可以将该对象完全从 Inspector 值中排除。该字段由 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")将从检查器输出中完全移除。如果对其他对象属性定义了任何验证规则并且所需属性为空,这些规则将被忽略。
编辑器的 default 值,如果指定,应该是一个具有与在 properties 配置参数中定义的属性相同的对象。
public function defineProperties()
{
return [
'address' => [
'title' => 'Address',
'type' => 'object',
'properties' => [/*...*/],
'default' => [
'streetAddress' => '321-210 Second ave',
'city' => 'Springfield',
'country' => 'us'
]
]
];
}