检查器类型是CMS组件使用的属性类型。它们在以下区域中被引用:
所有检查器类型都以它们各自的 type 属性进行标识。
public function defineProperties()
{
return [
'maxItems' => [
'title' => 'Max Items',
'type' => 'string'
]
];
}以下检查器类型可用:
字符串
字符串列表
文本
自动完成
复选框
下拉菜单
字典
对象
对象列表
集合
属性参数由一个包含以下键的数组定义。
| 键 | 说明 |
|---|---|
| title | 必填,属性标题,在 CMS 后台的组件检查器中显示。 |
| description | 必填,属性描述,在 CMS 后台的组件检查器中显示。 |
| default | 可选,添加组件到页面或布局时使用的默认属性值。 |
| type | 指定属性类型,决定该属性在检查器中的展示方式。 |
| validation | 可选,指定属性值的验证规则(详见下文)。 |
| placeholder | 可选,字符串和下拉属性的占位符。 |
| options | 可选,下拉菜单属性可用的选项数组。 |
| optionsMethod | 指定组件类中的方法名来获取选项。 |
| depends | 下拉属性所依赖的属性名称数组。详见下拉类型。 |
| group | 可选分组名称。分组会在检查器中创建分区,优化用户体验。多个属性使用同一分组名可将其归入同一区块。 |
| showExternalParam | 指定该属性在检查器中是否显示外部参数编辑器。默认值:true。 |
| ignoreIfDefault | 设为 true 时,如果选项等于默认值会被从数组输出中排除。默认值:false。 |
| ignoreIfEmpty | 设为 true 时,如果选项为空值会被从数组输出中排除。默认值:false。 |
| sortOrder | 指定该属性在可用列表中的自定义整数位置。 |
检查器类型支持可应用于属性的多种验证规则。验证规则可应用于顶级属性以及对象和对象列表编辑器的内部属性定义。
public function defineProperties()
{
return [
'name' => [
'title' => 'Name',
'type' => 'string',
'validation' => [
'required' => [
'message' => 'The Name field is required'
],
'regex' => [
'message' => 'The Name field can contain only Latin letters.',
'pattern' => '^[a-zA-Z]+$'
]
]
]
];
}validation 对象中的键值对应一种验证器(见下文)。每种验证器通过对象进行配置,每种验证器拥有不同的属性。其中 message 属性是所有验证器通用的。
required(必填)验证器用于检查值是否为空。该验证器可用于任何编辑器,包括复杂编辑器(如集合、字典、对象列表等)。示例:
public function defineProperties()
{
return [
'name' => [
'title' => 'Name',
'type' => 'string',
'validation' => [
'required' => [
'message' => 'The Name field is required'
]
]
]
];
}此 regex 验证器使用正则表达式验证字符串值。此验证器只能用于字符串类型的编辑器。示例:
public function defineProperties()
{
return [
'name' => [
'title' => 'Name',
'type' => 'string',
'validation' => [
'regex' => [
'message' => 'The Name field can contain only Latin letters',
'pattern' => '^[a-z]+$',
'modifiers' => 'i'
]
]
]
];
}正则表达式通过必需的 pattern 参数指定。modifiers 参数是可选的,用于设置正则表达式的修饰符。
integer(整数)验证器用于检查该值是否为整数,并且可以选择性地验证该值是否在指定区间内。该验证器只能用于字符串类型的编辑器。示例:
public function defineProperties()
{
return [
'numOfColumns' => [
'title' => 'Number of Columns',
'type' => 'string',
'validation' => [
'integer' => [
'message' => 'The Number of Columns field should contain an integer value',
'allowNegative' => true,
'min' => [
'value' => -10,
'message' => 'The number of columns should not be less than -10.'
],
'max' => [
'value' => 10,
'message' => 'The number of columns should not be greater than 10.'
]
]
]
]
];
}支持的参数:
allowNegative - 可选的, 决定是否允许负值. 默认情况下不允许负值.min - 可选对象,定义了允许的最小值和错误消息。对象字段:
value - 定义最小值message - 可选, 定义错误消息。max - 可选对象,定义允许的最大值和错误消息。对象字段:
value - 定义最大值。message - 可选,定义错误消息.该 float 验证器检查该值是否为浮点数。此验证器的参数与上述整数验证器的参数匹配。示例:
public function defineProperties()
{
return [
'amount' => [
'title' => 'Amount',
'type' => 'string',
'validation' => [
'float' => [
'message' => 'The Amount field should contain a positive floating point value'
]
]
]
];
}有效的浮点数格式:
allowNegative 为 true)allowNegative 为 true)length 验证器检查字符串、数组或对象的长度是否不短于或不长于指定值。该验证器可以与字符串、文本、集合、字符串列表、字典和对象列表编辑器配合使用。在多值编辑器 (集合、字符串列表、字典和对象列表) 中,它验证编辑器中创建的项的数量。
length验证器不验证空值。例如,如果将其应用于集合编辑器,并且集合为空,则无论min和max参数值如何,验证都将通过。将required验证器与length验证器一起使用,以确保在应用长度验证之前值不为空。
public function defineProperties()
{
return [
'name' => [
'title' => 'Name',
'type' => 'string',
'validation' => [
'length' => [
'min' => [
'value' => 2,
'message' => 'The name should not be shorter than two letters.'
],
'max' => [
'value' => 10,
'message' => 'The name should not be longer than 10 letters.'
]
]
]
]
];
}支持的参数:
min - 可选对象,定义允许的最小长度和错误消息。对象字段:
value - 定义了最小值。message - 可选,定义错误消息。max - 可选对象,定义允许的最大长度和错误消息。对象字段:
value - defines the maximum value.message - 可选,定义错误消息。