检查器类型是 CMS 组件使用的属性类型。这些被以下区域引用:
所有的检查器类型都被识别为它们各自的 type 属性。
public function defineProperties()
{
return [
'maxItems' => [
'title' => 'Max Items',
'type' => 'string'
]
];
}以下检查器类型可用:
字符串
字符串列表
文本
自动完成
复选框
下拉菜单
字典
对象
对象列表
集合
属性参数使用一个包含以下键的数组进行定义。
| Key | Description |
|---|---|
| title | required, the property title, it is used by the component Inspector in the CMS backend. |
| description | required, the property description, it is used by the component Inspector in the CMS backend. |
| default | optional, the default property value to use when the component is added to a page or layout in the CMS backend. |
| type | specifies the property type, which defines how the property is displayed in the Inspector. |
| validation | optional, specifies validation rules for the property value (see below). |
| placeholder | optional placeholder for string and dropdown properties. |
| options | optional array of options for dropdown properties. |
| optionsMethod | specify a method name on the component class to source options. |
| depends | an array of property names a dropdown property depends on. See the dropdown type for more information. |
| group | an optional group name. Groups create sections in the Inspector simplifying the user experience. Use a same group name in multiple properties to combine them. |
| showExternalParam | specifies visibility of the external parameter editor for the property in the Inspector. Default: true. |
| ignoreIfDefault | set to true to exclude the output from the array if the selection matches default value. Default: false |
| ignoreIfEmpty | set to true to exclude the output from the array if the selection has an empty value. Default: false |
| sortOrder | specify a custom position as an integer for the property in the available list. |
检查器类型支持若干可应用于属性的验证规则。验证规则可应用于顶层属性,以及对象和对象列表编辑器的内部属性定义。
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]+
The key value in the `validation` object refers to a validator (see below). Validators are configured with objects, which properties depend on a validator. One property - `message` is common for all validators.
### Required Validator
The `required` validator checks if a value is not empty. The validator can be used with any editor, including complex editors (sets, dictionaries, object lists, etc.). Example:
```php
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]+
The regular expression is specified with the required `pattern` parameter. The `modifiers` parameter is optional and can be used for setting regular expression modifiers.
### Integer Validator
The `integer` validator checks if the value is integer and can optionally validate if the value is within a specific interval. The validator can be used only with string-typed editors. Example:
```php
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 - 可选对象,定义允许的最小长度和错误消息。对象字段:
值 - 定义最小值。message - 可选, 定义错误消息。max - 可选对象,定义允许的最大长度和错误消息。对象字段:
value - 定义了最大值。
message - 可选的,定义错误消息。
]
]
]
];
}
The key value in the `validation` object refers to a validator (see below). Validators are configured with objects, which properties depend on a validator. One property - `message` is common for all validators.
### Required Validator
The `required` validator checks if a value is not empty. The validator can be used with any editor, including complex editors (sets, dictionaries, object lists, etc.). Example:
### Regex Validator
The `regex` validator validates string values with a regular expression. The validator can be used only with string-typed editors. Example:
The regular expression is specified with the required `pattern` parameter. The `modifiers` parameter is optional and can be used for setting regular expression modifiers.
### Integer Validator
The `integer` validator checks if the value is integer and can optionally validate if the value is within a specific interval. The validator can be used only with string-typed editors. Example:
Supported parameters:
* `allowNegative` - optional, determines if negative values are allowed. By default negative values are not allowed.
* `min` - optional object, defines the minimum allowed value and error message. Object fields:
* `value` - defines the minimum value.
* `message` - optional, defines the error message.
* `max` - optional object, defines the maximum allowed value and error message. Object fields:
* `value` - defines the maximum value.
* `message` - optional, defines the error message.
### Float Validator
The `float` validator checks if the value is a floating point number. The parameters for this validator match the parameters of the **integer** validator described above. Example:
Valid floating point number formats:
* 10
* 10.302
* -10 (if `allowNegative` is `true`)
* -10.84 (if `allowNegative` is `true`)
### Length Validator
The `length` validator checks if a string, array or object is not shorter or longer than specified values. This validator can work with the string, text, set, string list, dictionary and object list editors. In multiple-value editors (set, string list, dictionary and object list) it validates the number of items created in the editor.
> The `length` validator doesn't validate empty values. For example, if it's applied to a set editor, and the set is empty, the validation will pass regardless of the `min` and `max` parameter values. Use the `required` validator together with the `length` validator to make sure that the value is not empty before the length validation is applied.
Supported parameters:
* `min` - optional object, defines the minimum allowed length and error message. Object fields:
* `value` - defines the minimum value.
* `message` - optional, defines the error message.
* `max` - optional object, defines the maximum allowed length and error message. Object fields:
* `value` - defines the maximum value.
* `message` - optional, defines the error message.
,
'modifiers' => 'i'
]
]
]
];
}
正则表达式是使用必需的 pattern 参数指定的。可选的 modifiers 参数可用于设置正则表达式修饰符。
该整型验证器检查值是否为整数,并且可以可选地验证值是否在特定区间内。该验证器只能用于字符串类型的编辑器。示例:
代码块4
支持的参数:
allowNegative - 可选,决定是否允许负值。默认情况下不允许负值。min - 可选对象,定义允许的最小值和错误消息。对象字段:
value - 定义最小值。message - 可选,定义错误消息。max - 可选对象,定义允许的最大值和错误消息。对象字段:
value - 定义最大值。message - 可选,定义错误消息。float 验证器检查值是否为浮点数。此验证器的参数与上述描述的 integer 验证器的参数一致。示例:
代码块5
有效的浮点数格式:
allowNegative 为 true)allowNegative 为 true)length 验证器检查字符串、数组或对象的长度是否不短于或长于指定值。此验证器可与字符串、文本、集合、字符串列表、字典和对象列表编辑器配合使用。在多值编辑器(集合、字符串列表、字典和对象列表)中,它验证在编辑器中创建的项的数量。
length验证器不验证空值。例如,如果它应用于一个集合编辑器,并且该集合为空,那么无论min和max参数值如何,验证都将通过。将required验证器与length验证器一起使用,以确保在应用长度验证之前,该值不为空。
代码块 6
支持的参数:
min - 可选对象,定义了允许的最小长度和错误消息。对象字段:
value - 定义最小值。消息 - 可选的,定义错误消息。max - 可选对象,定义允许的最大长度和错误消息。对象字段:
value - 定义最大值。
message - 可选的,定义错误消息。
]
]
]
];
}
The key value in the `validation` object refers to a validator (see below). Validators are configured with objects, which properties depend on a validator. One property - `message` is common for all validators.
### Required Validator
The `required` validator checks if a value is not empty. The validator can be used with any editor, including complex editors (sets, dictionaries, object lists, etc.). Example:
### Regex Validator
The `regex` validator validates string values with a regular expression. The validator can be used only with string-typed editors. Example:
The regular expression is specified with the required `pattern` parameter. The `modifiers` parameter is optional and can be used for setting regular expression modifiers.
### Integer Validator
The `integer` validator checks if the value is integer and can optionally validate if the value is within a specific interval. The validator can be used only with string-typed editors. Example:
Supported parameters:
* `allowNegative` - optional, determines if negative values are allowed. By default negative values are not allowed.
* `min` - optional object, defines the minimum allowed value and error message. Object fields:
* `value` - defines the minimum value.
* `message` - optional, defines the error message.
* `max` - optional object, defines the maximum allowed value and error message. Object fields:
* `value` - defines the maximum value.
* `message` - optional, defines the error message.
### Float Validator
The `float` validator checks if the value is a floating point number. The parameters for this validator match the parameters of the **integer** validator described above. Example:
Valid floating point number formats:
* 10
* 10.302
* -10 (if `allowNegative` is `true`)
* -10.84 (if `allowNegative` is `true`)
### Length Validator
The `length` validator checks if a string, array or object is not shorter or longer than specified values. This validator can work with the string, text, set, string list, dictionary and object list editors. In multiple-value editors (set, string list, dictionary and object list) it validates the number of items created in the editor.
> The `length` validator doesn't validate empty values. For example, if it's applied to a set editor, and the set is empty, the validation will pass regardless of the `min` and `max` parameter values. Use the `required` validator together with the `length` validator to make sure that the value is not empty before the length validation is applied.
Supported parameters:
* `min` - optional object, defines the minimum allowed length and error message. Object fields:
* `value` - defines the minimum value.
* `message` - optional, defines the error message.
* `max` - optional object, defines the maximum allowed length and error message. Object fields:
* `value` - defines the maximum value.
* `message` - optional, defines the error message.