使用JSON模式作为表单模型来创作自适应表单,需要对JSON模式有基本的了解。 建议在本文之前阅读以下内容。
AEM Forms支持使用现有JSON模式作为表单模型来创建自适应表单。 此JSON模式表示组织内的后端系统在其中生成或使用数据的结构。 您使用的JSON架构应与 v4规范.
使用JSON架构的主要功能包括:
JSON模式由简单和复杂的元素类型组成。 元素具有可向元素添加规则的属性。 将这些元素和属性拖动到自适应表单上后,会自动将它们映射到相应的自适应表单组件。
JSON元素与自适应表单组件的映射如下所示:
JSON元素、属性或属性 | 自适应表单组件 |
---|---|
具有enum和enumNames约束的字符串属性。 语法,
|
下拉组件:
|
具有格式约束的字符串属性。 例如,电子邮件和日期。 语法,
|
|
{ "type" :"string", } |
文本字段 |
数字属性 |
子类型设置为浮点的数值字段 |
整数属性 |
子类型设置为整数的数字字段 |
布尔属性 |
开关 |
对象属性 |
面板 |
array属性 | 最小值和最大值分别等于minItems和maxItems的可重复面板。 仅支持同构阵列。 因此,项目约束必须是对象而不是数组。 |
自适应表单使用JSON模式中可用的信息来映射每个生成的字段。 特别是:
以下是JSON模式的示例。
{
"$schema": "https://json-schema.org/draft-04/schema#",
"definitions": {
"employee": {
"type": "object",
"properties": {
"userName": {
"type": "string"
},
"dateOfBirth": {
"type": "string",
"format": "date"
},
"email": {
"type": "string",
"format": "email"
},
"language": {
"type": "string"
},
"personalDetails": {
"$ref": "#/definitions/personalDetails"
},
"projectDetails": {
"$ref": "#/definitions/projectDetails"
}
},
"required": [
"userName",
"dateOfBirth",
"language"
]
},
"personalDetails": {
"type": "object",
"properties": {
"GeneralDetails": {
"$ref": "#/definitions/GeneralDetails"
},
"Family": {
"$ref": "#/definitions/Family"
},
"Income": {
"$ref": "#/definitions/Income"
}
}
},
"projectDetails": {
"type": "array",
"items": {
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
},
"projects": {
"$ref": "#/definitions/projects"
}
}
},
"minItems": 1,
"maxItems": 4
},
"projects": {
"type": "array",
"items": {
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
},
"projectsAdditional": {
"$ref": "#/definitions/projectsAdditional"
}
}
},
"minItems": 1,
"maxItems": 4
},
"projectsAdditional": {
"type": "array",
"items": {
"properties": {
"Additional_name": {
"type": "string"
},
"Additional_areacode": {
"type": "number"
}
}
},
"minItems": 1,
"maxItems": 4
},
"GeneralDetails": {
"type": "object",
"properties": {
"age": {
"type": "number"
},
"married": {
"type": "boolean"
},
"phone": {
"type": "number",
"aem:afProperties": {
"sling:resourceType": "/libs/fd/af/components/guidetelephone",
"guideNodeClass": "guideTelephone"
}
},
"address": {
"type": "string"
}
}
},
"Family": {
"type": "object",
"properties": {
"spouse": {
"$ref": "#/definitions/spouse"
},
"kids": {
"$ref": "#/definitions/kids"
}
}
},
"Income": {
"type": "object",
"properties": {
"monthly": {
"type": "number"
},
"yearly": {
"type": "number"
}
}
},
"spouse": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"Income": {
"$ref": "#/definitions/Income"
}
}
},
"kids": {
"type": "array",
"items": {
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
}
},
"minItems": 1,
"maxItems": 4
}
},
"type": "object",
"properties": {
"employee": {
"$ref": "#/definitions/employee"
}
}
}
定义键用于标识可重用的架构。 可重用架构定义用于创建片段。 它类似于在XSD中标识复杂类型。 下面提供了一个具有定义的JSON模式示例:
{
"$schema": "https://json-schema.org/draft-04/schema#",
"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
},
"required": ["street_address", "city", "state"]
}
},
"type": "object",
"properties": {
"billing_address": { "$ref": "#/definitions/address" },
"shipping_address": { "$ref": "#/definitions/address" }
}
}
上例定义了客户记录,其中每个客户都同时具有送货地址和帐单地址。 两个地址的结构相同 — 地址具有街道地址、城市地址和州地址 — 因此最好不要重复这些地址。 此外,还可以轻松添加和删除字段,以便将来进行任何更改。
您可以使用 aem:afProperties 属性来预配置要映射到自定义自适应表单组件的JSON架构字段。 下面列出了一个示例:
{
"properties": {
"sizeInMB": {
"type": "integer",
"minimum": 16,
"maximum": 512,
"aem:afProperties" : {
"sling:resourceType" : "/apps/fd/af/components/guideTextBox",
"guideNodeClass" : "guideTextBox"
}
}
},
"required": [ "sizeInMB" ],
"additionalProperties": false
}
您可以向JSON架构元素添加以下限制,以限制自适应表单组件可接受的值:
架构属性 |
数据类型 |
描述 |
组件 |
|
字符串 |
指定数值和日期的上限。 默认情况下,包含最大值。 |
|
|
字符串 |
指定数值和日期的下限。 默认情况下,包含最小值。 |
|
|
布尔值 |
如果为true,则在表单组件中指定的数值或日期必须小于为maximum属性指定的数值或日期。 如果为false,则表单组件中指定的数值或日期必须小于或等于为最大属性指定的数值或日期。 |
|
|
布尔值 |
如果为true,则表单组件中指定的数值或日期必须大于为最小属性指定的数值或日期。 如果为false,则表单组件中指定的数值或日期必须大于或等于为最小属性指定的数值或日期。 |
|
|
字符串 |
指定组件中允许的最小字符数。 最小长度必须等于或大于零。 |
|
maxLength |
字符串 | 指定组件中允许的最大字符数。 最大长度必须等于或大于零。 |
|
|
字符串 |
指定字符的顺序。 如果字符符合指定的模式,则组件接受字符。 模式属性映射到相应自适应表单组件的验证模式。 |
|
maxItems | 字符串 | 指定数组中的最大项数。 最大项目必须等于或大于零。 | |
minItems | 字符串 | 指定数组中项的最小数量。 最小项目必须等于或大于零。 |
自适应表单不支持以下JSON模式构建:
为什么我无法为可重复的子表单(minOccours或maxOccurs值大于1)拖动子表单的单个元素(从任何复杂类型生成的结构)?
在可重复的子表单中,您必须使用完整的子表单。 如果只希望选择字段,请使用整个结构并删除不需要的字段。
我在内容查找器中有一个很长的复杂结构。 如何查找特定元素?
您有两个选项: