Using a JSON Schema as form model

AEM Forms supports creation of an adaptive form by using an existing JSON Schema as the form model. This JSON Schema represents the structure in which data is produced or consumed by the back-end system in your organization. The JSON Schema you use should be compliant with v4 specifications.

The key features of using an JSON Schema are:

The structure of the JSON is displayed as a tree in the Content Finder tab in the authoring mode for an adaptive form. You can drag and add element from the JSON hierarchy to the adaptive form.

You can pre-populate the form using JSON that is compliant with the associated schema.

On submission, the data entered by the user is submitted as JSON that aligns with the associated schema.

An JSON Schema consists of simple and complex element types. The elements have attributes that add rules to the element. When these elements and attributes are dragged onto an adaptive form, they are automatically mapped to the corresponding adaptive form component.

This mapping of JSON elements with adaptive form components is as follows:

JSON element, properties, or attributes

Adaptive form component

String properties with enum and enumNames constraint.

Syntax,

{

"type" : "string",

"enum" : ["M", "F"]

"enumNames" : ["Male", "Female"]

}

Drop down component:

Values listed in enumNames are displayed in the drop box.

Values listed in the enum are used for calculation.

String property with format constraint. For example, email, and date.

Syntax,

{

"type" : "string",

"format" : "email"

}

Email component is mapped when the type is string and format is email.

Textbox component with validation is mapped when the type is string and format is hostname.

{

"type" : "string",

}

Text field

number property

Numeric Field with sub type set to float

integer property

Numeric Field with sub type set to integer

boolean property

Switch

object property

Panel

array property

Repeatable Panel with min and max equal to minItems and maxItems respectively. Only Homogenous arrays are supported. So the items constraint must be an object and not an array.

Common schema properties

Adaptive Form uses information available in JSON Schema to map each generated field. In particular:

The title property serves as label for the adaptive form components.

The description property is set as long description for an adaptive form component.

The default property serves as initial value of an adaptive form field.

The maxLength property is set as maxlength attribute of the text field component.

The minimum, maximum, exclusiveMinimum, and exclusiveMaximum properties are used for Numeric box component.

To support range for DatePicker component additional JSON Schema properties minDate and maxDate are provided..

The minItems and maxItems properties are used to restrict the number of items/fields that may be added or removed from a panel component.

The readOnly property sets the readonly attribute of an adaptive form component.

The required property marks the adaptive form field as mandatory whereas in case of panel(where type is object), the final submitted JSON data has fields with empty value corresponding to that object.

The pattern property is set as the validation pattern (regular expression) in adaptive form.

Reusable schema definitions

Definition keys are used to identify reusable schemas. The reusable schema definitions are used to create fragments. It is similar to identifying complex types in XSD. A sample JSON Schema with definitions is given below:

The above example defines a customer record, where each customer has both a shipping and a billing address. Structure of both the addresses is same—addresses have a street address, city and state— so it is a good idea to not duplicate the addresses. It also makes addition and deletion of fields easy for any future changes.

Pre-Configuring fields in JSON Schema Definition

You can use the aem:afProperties property to preconfigure JSON Schema field to map to a custom adaptive form component. An example is listed below: