使用JSON模式作为表单模型创作自适应表单需要基本了解JSON模式。 建议在本文之前阅读以下内容。
AEM Forms支持使用现有JSON模式作为表单模型创建自适应表单。 此JSON模式表示组织中的后端系统生成或使用数据的结构。 您使用的JSON模式应符合v4规范。
使用JSON模式的主要功能有:
JSON模式由简单和复杂的元素类型组成。 元素具有向元素添加规则的属性。 当这些元素和属性被拖动到自适应表单上时,它们会自动映射到相应的自适应表单组件。
JSON元素与自适应表单组件的映射如下:
JSON元素、属性或属性 | 自适应表单组件 |
---|---|
具有enum和enumNames约束的字符串属性。 语法,
|
下拉组件:
|
具有格式约束的字符串属性。 例如,电子邮件和日期。 语法,
|
|
{ “类型”:"string", } |
文本字段 |
number属性 |
子类型设置为float 的数字字段 |
整数属性 |
子类型设置为整数 的数字字段 |
布尔属性 |
切换 |
对象属性 |
面板 |
数组属性 | 最小和最大分别等于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,则表单组件中指定的数字值或日期必须小于或等于为maximum属性指定的数字值或日期。 |
|
|
布尔型 |
如果为true,则表单组件中指定的数字值或日期必须大于为最小属性指定的数字值或日期。 如果为false,则表单组件中指定的数字值或日期必须大于或等于为最小属性指定的数字值或日期。 |
|
|
字符串 |
指定组件中允许的最少字符数。 最小长度必须等于或大于零。 |
|
maxLength |
字符串 | 指定组件中允许的最大字符数。 最大长度必须等于或大于零。 |
|
|
字符串 |
指定字符的顺序。 如果字符符合指定的模式,则组件接受这些字符。 模式属性映射到相应自适应表单组件的验证模式。 |
|
maxItems | 字符串 | 指定数组中最大项数。 最大项目必须等于或大于零。 | |
minItems | 字符串 | 指定数组中最小项数。 最小项目必须等于或大于零。 |
自适应表单不支持以下JSON模式构造:
为什么我无法拖动子表单的单个元素(从任何复杂类型生成的结构)以用于可重复的子表单(minOccours或maxOccurs值大于1)?
在可重复的子表单中,必须使用完整的子表单。 如果只想选择字段,请使用整个结构并删除不需要的字段。
我在内容查找器中有一个很长的复杂结构。如何找到特定元素?
您有两种选择: