Prefill adaptive form fields prefill-adaptive-form-fields

CAUTION
AEM 6.4 has reached the end of extended support and this documentation is no longer updated. For further details, see our technical support periods. Find the supported versions here.

Use existing data to prefill fields of an adaptive form.

Introduction introduction

You can prefill the fields of an adaptive form using existing data. When a user opens a form, the values for those fields are prefilled. To prefill data in an adaptive form, make the user data available as a prefill XML / JSON in the format that adheres to prefill data structure of adaptive forms.

Structure of prefill data the-prefill-structure

An adaptive form can have mix of bound and unbound fields. Bound fields are fields which are dragged from the Content Finder tab and contain non-empty bindRef property value in the field edit dialog. Unbound fields are dragged directly from the component browser of Sidekick and have an empty bindRef value.

You can prefill both bound and unbound fields of an adaptive form. The prefill data contains the afBoundData and afUnBoundData sections to prefill both bound and unbound fields of an adaptive form. The afBoundData section contains the prefill data for bound fields and panels. This data must be compliant with the associated form model schema:

  • For adaptive forms using the XFA form template, use the prefill XML compliant with the data schema of the XFA template.
  • For adaptive forms using XML schema, use the prefill XML compliant with the XML schema structure.
  • For adaptive forms using JSON schema, use the prefill JSON compliant with the JSON schema.
  • For adaptive forms using FDM schema, use the prefill JSON compliant with FDM schema.
  • For adaptive forms with no form model, there is no bound data. Every field is an unbound field and is prefilled using the unbound XML.

Sample Prefill XML structure sample-prefill-xml-structure

<?xml version="1.0" encoding="UTF-8"?>
<afData>
  <afBoundData>
     <employeeData>
        .
     </employeeData>
  </afBoundData>

  <afUnboundData>
    <data>
      <textbox>Hello World</textbox>
         .
         .
      <numericbox>12</numericbox>
         .
         .
    </data>
  </afUnboundData>
</afData>

Sample Prefill JSON structure sample-prefill-json-structure

{
   "afBoundData": {
      "employeeData": { }
   },
   "afUnboundData": {
      "data": {
         "textbox": "Hello World",
         "numericbox": "12"
      }
   }
}

For bound fields with same bindref or unbound fields with same name, data specified in the XML tag or JSON object is filled in all the fields. For example, two fields in a form are mapped to the name textbox in the prefill data. During runtime, if the first text box field contains “A”, then “A” is automatically filled in the second text box. This linking is called live linking of adaptive form fields.

Adaptive form using XFA form template xfa-based-af

The structure of prefill XML and the submitted XML for XFA-based adaptive forms is as follows:

  • Prefill XML Structure: The prefill XML for XFA-based adaptive form must be compliant with the data schema of the XFA form template. To prefill unbound fields, wrap the prefill XML structure into /afData/afBoundData tag.

  • Submitted XML Structure: When no prefill XML is used, the submitted XML contains data for both bound and unbound fields in afData wrapper tag. If a prefill XML is used, the submitted XML has the same structure as the prefill XML. If the prefill XML starts with the afData root tag, the output XML also has the same format. If the prefill XML does not have afData/afBoundDatawrapper and instead starts directly from the schema root tag like employeeData, the submitted XML also starts with the employeeData tag.

Prefill-Submit-Data-ContentPackage.zip

Get File
Sample containing prefill data and submitted data

XML schema-based adaptive forms  xml-schema-af

The structure of prefill XML and submitted XML for adaptive forms based on XML schema is as follows:

  • Prefill XML structure: The prefill XML must be compliant to associated XML Schema. To prefill unbound fields, wrap the prefill XML structure into /afData/afBoundData tag.
  • Submitted XML structure: if no prefill XML is used, the submitted XML contains data for both bound and unbound fields in afData wrapper tag. If the prefill XML is used, the submitted XML has the same structure as the prefill XML. If the prefill XML starts with the afData root tag, the output XML has the same format. If the prefill XML does not have afData/afBoundData wrapper and instead start directly from the schema root tag like employeeData, the submitted XML also starts with the employeeData tag.
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="https://adobe.com/sample.xsd"
            xmlns="https://adobe.com/sample.xsd"
            xmlns:xs="https://www.w3.org/2001/XMLSchema">

    <xs:element name="sample" type="SampleType"/>

    <xs:complexType name="SampleType">
        <xs:sequence>
            <xs:element name="noOfProjectsAssigned" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

For fields whose model is XML schema, the data is prefilled in the afBoundData tag as shown in the sample XML below. It can be used for prefilling an adaptive form with one or more unbound text fields.

<?xml version="1.0" encoding="UTF-8"?><afData>
  <afUnboundData>
    <data>
      <textbox>Ignorance is bliss :) </textbox>
    </data>
  </afUnboundData>
  <afBoundData>
    <data>
      <noOfProjectsAssigned>twelve</noOfProjectsAssigned>
    </data>
  </afBoundData>
</afData>
NOTE
It is recommended not to use unbound fields in bound panels (panels with non-empty bindRef that has been created by dragging components from Sidekick or Data Sources tab). It may cause loss of data of these unbound fields. Also, it is recommended that the names of the fields are unique across the form, specially for unbound fields.

An example without afData and afBoundData wrapper an-example-without-afdata-and-afbounddata-wrapper

<?xml version="1.0" encoding="UTF-8"?><config>
 <assignmentDetails descriptionOfAssignment="Some Science Project" durationOfAssignment="34" financeRelatedProject="1" name="Lisa" numberOfMentees="1"/>
 <assignmentDetails descriptionOfAssignment="Kidding, right?" durationOfAssignment="4" financeRelatedProject="1" name="House" numberOfMentees="3"/>
</config>

JSON schema-based adaptive forms json-schema-based-adaptive-forms

For adaptive forms based on JSON schema, the structure of prefill JSON and submitted JSON is described below. For more information, see Creating adaptive forms using JSON schema.

  • Prefill JSON structure: The prefill JSON must be compliant with the associated JSON Schema. Optionally, it can be wrapped into the /afData/afBoundData Object if you want to prefill unbound fields as well.
  • Submitted JSON structure: if no prefill JSON is used, the submitted JSON contains data for both bound and unbound fields in afData wrapper tag. If the prefill JSON is used, the submitted JSON has the same structure as the prefill JSON. If the prefill JSON starts with the afData root object, the output JSON has the same format. If the prefill JSON does not have afData/afBoundData wrapper and instead starts directly from the schema root object such as user, the submitted JSON also starts with the user object.
{
    "id": "https://some.site.somewhere/entry-schema#",
    "$schema": "https://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "address": {
            "type": "object",
            "properties": {
    "name": {
     "type": "string"
    },
    "age": {
     "type": "integer"
}}}}}

For fields which use JSON schema model, the data is prefilled in the afBoundData object as shown in the sample JSON below. It can be used for prefilling an adaptive form with one or more unbound text fields. Below is an example of data with afData/afBoundData wrapper:

{
  "afData": {
    "afUnboundData": {
      "data": { "textbox": "Ignorance is bliss :) " }
    },
    "afBoundData": {
      "data": { {
   "user": {
    "address": {
     "city": "Noida",
     "country": "India"
}}}}}}}

Below is an example without afData/afBoundData wrapper:

{
 "user": {
  "address": {
   "city": "Noida",
   "country": "India"
}}}
NOTE
Using unbound fields in bound panels (panels with non-empty bindRef that have been created by dragging components from the Sidekick or Data Sources tab) is not recommended as it might cause loss of data of the unbound fields. It is recommended to have unique field names across the form, especially for unbound fields.

Adaptive Form with no form model adaptive-form-with-no-form-model

For adaptive forms with no form model, the data for all the fields is under the <data> tag of <afUnboundData> tag.

Also, take note of the following:

The XML tags for the user data submitted for various fields are generated using the name of the fields. Therefore, the field names must be unique.

<?xml version="1.0" encoding="UTF-8"?><afData>
  <afUnboundData>
    <data>
      <radiobutton>2</radiobutton>
      <repeatable_panel_no_form_model>
        <numericbox>12</numericbox>
      </repeatable_panel_no_form_model>
      <repeatable_panel_no_form_model>
        <numericbox>21</numericbox>
      </repeatable_panel_no_form_model>
      <checkbox>2</checkbox>
      <textbox>Nopes</textbox>
    </data>
  </afUnboundData>
  <afBoundData/>
</afData>

Configuring prefill service using Configuration Manager configuring-prefill-service-using-configuration-manager

To enable prefill service, specify the Default Prefill Service Configuration in the AEM Web Console Configuration. Use the following steps to configure the Prefill service:

NOTE
Prefill Service Configuration is applicable for adaptive forms, HTML5 forms, and HTML5 form sets.
  1. Open Adobe Experience Manager Web Console Configuration by using the URL:
    https://<server>:<port>/system/console/configMgr

  2. Search and open Default Prefill Service Configuration.

    prefill_config

  3. Enter the data location or a regex (regular expression) for the Data files locations. Examples of valid Data files locations are:

    • file:///C:/Users/public/Document/Prefill/.*
    • http://localhost:8000/somesamplexmlfile.xml
    note note
    NOTE
    By default, prefill is allowed through crx files for all types of Adaptive Forms (XSD, XDP, JSON, FDM, and no Form Model based). Prefill is allowed only with JSON and XML files.
  4. The prefill service is now configured for your form.

    note note
    NOTE
    The crx protocol takes care of prefilled data security and hence, is allowed by default. Prefilling via other protocols using generic regex might cause vulnerability. In the configuration, specify a secure URL configuration for protecting your data.

The curious case of repeatable panels the-curious-case-of-repeatable-panels

Generally, bound (form schema) and unbound fields are authored in the same adaptive form, but the following are a few exceptions in case the bound are repeatable:

  • Unbound repeatable panels are not supported for adaptive forms using the XFA form template, XSD, JSON schema, or FDM schema.
  • Do not use unbound fields in bound repeatable panels.
NOTE
As a rule of thumb, do not mix bound and unbound fields if they are intersected in data filled by the end user in unbound fields. If possible, you should modify the schema or the XFA form template and add an entry for unbound fields, so that it also becomes bound and its data is available like other fields in submitted data.

Supported protocols for prefilling user data supported-protocols-for-prefilling-user-data

Adaptive forms can be prefilled with user data in prefill data format via the following protocols when configured with valid regex:

The crx:// protocol the-crx-protocol

http://localhost:4502/content/forms/af/xml.html?wcmmode=disabled&dataRef=crx:///tmp/fd/af/myassets/sample.xml

The specified node must have a property called jcr:data and hold the data.

The file:// protocol  the-file-protocol-nbsp

http://localhost:4502/content/forms/af/someAF.html?wcmmode=disabled&dataRef=file:///C:/Users/form-user/Downloads/somesamplexml.xml

The referred file must be on the same server.

The https:// protocol the-http-protocol

http://localhost:4502/content/forms/af/xml.html?wcmmode=disabled&dataRef=http://localhost:8000/somesamplexmlfile.xml

The service:// protocol the-service-protocol

http://localhost:4502/content/forms/af/abc.html?wcmmode=disabled&dataRef=service://[SERVICE_NAME]/[IDENTIFIER]
  • SERVICE_NAME refers to the name of the OSGI prefill service. Refer Create and run a prefill service.
  • IDENTIFIER refers to any metadata required by the OSGI prefill service to fetch the prefill data. An identifier to the logged-in user is an example of metadata that could be used.
NOTE
Passing authentication parameters is not supported.

Setting data attribute in slingRequest setting-data-attribute-in-slingrequest

You can also set the data attribute in slingRequest, where the data attribute is a string containing XML or JSON, as shown in the sample code below (Example is for XML):

<%
           String dataXML="<afData>" +
                            "<afUnboundData>" +
                                "<data>" +
                                    "<first_name>"+ "Tyler" + "</first_name>" +
                                    "<last_name>"+ "Durden " + "</last_name>" +
                                    "<gender>"+ "Male" + "</gender>" +
                                    "<location>"+ "Texas" + "</location>" +
                                    "</data>" +
                            "</afUnboundData>" +
                        "</afData>";
        slingRequest.setAttribute("data", dataXML);
%>

You can write a simple XML or JSON string containing all your data and set it in slingRequest. This can easily be done in your renderer JSP for any component, which you want to include in the page where you can set the slingRequest data attribute.

For example, where you want a specific design for your page with a specific type of header. To achieve this, you can write your own header.jsp, which you can include in your page component and set the data attribute.

Another good example is a use case where you would like to prefill data on login through social accounts like Facebook, Twitter, or LinkedIn. In this case, you can include a simple JSP in header.jsp, which fetches data from the user account and sets the data parameter.

prefill-page component.zip

Get File
Sample prefill.jsp in page component

AEM Forms custom prefill service aem-forms-custom-prefill-service

You can use custom prefill service for the scenarios, where you constantly read data from a pre-defined source. The prefill service reads data from defined data sources and prefills the fields of the adaptive form with the content of the prefill data file. It also helps you permanently associate prefilled data with an adaptive form.

Create and run a prefill service create-and-run-a-prefill-service

The prefill service is an OSGi service and is packaged through OSGi bundle. You create the OSGi bundle, upload, and install it to AEM Forms bundles. Before you get started with creating the bundle:

Create a prefill service create-a-prefill-service

The boilerplate package (sample prefill service package) contains sample implementation of AEM Forms prefill service. Open the boilerplate package in a code editor. For example, open the boilerplate project in Eclipse for editing. After you open the boilerplate package in a code editor, perform the following steps to create the service.

  1. Open the src\main\java\com\adobe\test\Prefill.java file for editing.

  2. In the code, set value of:

    • nodePath: The node path variable pointing to crx-repository location contains path of the data (prefill) file. For example, /content/prefilldata.xml
    • label: The label parameter specifies display name of the service. For example, Default Prefill Service
  3. Save and close the Prefill.java file.

  4. Add the AEM Forms Client SDK package to the build path of the boilerplate project.

  5. Compile the project and create the .jar for the bundle.

Start and use the prefill service start-and-use-the-prefill-service

To start the prefill service, upload the JAR file to AEM Forms Web Console, and activate the service. Now, the service starts appearing in adaptive forms editor. To associate a prefill service to an adaptive form:

  1. Open the adaptive form in Forms Editor and open the Properties panel for the Form Container.
  2. In the Properties console, navigate to AEM Forms container > Basic > Prefill Service.
  3. Select the Default Prefill Service and click Save. The service is associated to the form.
recommendation-more-help
a6ebf046-2b8b-4543-bd46-42a0d77792da