You can populate the fields of an Adaptive form using existing data. When a user opens a form, the values for those fields are prepopulated. You can achieve this with adaptive forms with or without a form template or XML schema as the form model. To accomplish this, the user data should be available as a pre-fill XML in a specific format that adheres to adaptive forms.
An adaptive form can have mix of bound and unbound fields. bound fields are those 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 Sidekick and have an empty bindRef value.
Adaptive forms support prepopulating of both bound and unbound fields. To achieve this, the prefill XML can include two sections - afBoundedData and afUnBoundedData. The afBoundedData section contains the pre-fill data for bound fields and panels. This data has to be compliant with the associated form model schema.
- For adaptive forms using the XFA form template, the pre-fill XML should be compliant with the data schema of the XFA template.
- For adaptive forms using the XML Schema, the pre-fill XML should be compliant with the schema structure.
- 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.
<?xml version="1.0" encoding="UTF-8"?> <afData> <afBoundData> <employeeData> . </employeeData> </afBoundData> <afUnboundData> <data> <textbox>Hello World</textbox> . . <numericbox>12</numericbox> . . </data> </afUnboundData> </afData>
- Prefill XML Structure: The pre-fill XML for XFA-based adaptive form must be compliant with the data schema of the XFA form template. Optionally, it can be wrapped into /afData/afBoundData tag if you want to prepopulate unbound fields as well.
- Submitted XML Structure: if no pre-fill XML is used, the submitted XML contains data for both bound and unbound fields in afData wrapper tag. If a pre-fill XML is used, the submitted XML has the same structure as the pre-fill XML. If the pre-fill XML starts with the afData root tag, the output XML also has the same format. If the pre-fill XML does not have afData/afBoundData wrapper and instead starts directly from the schema root tag like employeeData, the submitted XML also starts with the employeeData tag.
- Pre-fill XML structure: The pre-fill XML must be compliant to associated XML Schema. Optionally, it can be wrapped into the /afData/afBoundData tag if you want to prepopulate unbound fields as well.
- Submitted XML structure: if no pre-fill XML is used, the submitted XML contains data for both bound and unbound fields in afData wrapper tag. If the pre-fill XML is used, the submitted XML has the same structure as the pre-fill XML. If the pre-fill XML starts with the afData root tag, the output XML has the same format. If the pre-fill 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="http://adobe.com/sample.xsd" xmlns="http://adobe.com/sample.xsd" xmlns:xs="http://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 populated in the afBoundedData tag as shown in the sample schema below. It can be used for authoring 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>
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). 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.
<?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>
For adaptive forms with no form model, the data for all the fields will be under the <data> tag of whose parent is <afUnboundData>.
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.
- unbound fields are written in the folder created on submission and in the XML in the jcr:data property of that folder.
<?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>
In order to enable prefill service, you need to specify the Forms Common Configuration Service in the AEM Web Console Configuration. Use the following steps to configure the Prefill service:
Enter the data location or a regex (regular expression) for the Data files locations. Examples of valid Data files locations are:
Note: By default, prefill is allowed through crx files for all types of Adaptive Forms (none, XSD, and XDP). Prefill is allowed only with XML files.
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. A very secure URL must be specified in the configuration for protecting your data.
Generally, bound (from form template or XML schema) and unbound fields or subforms are authored in the same adaptive form, but the following are a few exceptions in case the bound subforms are repeatable:
- Unbound repeatable panels are not supported for adaptive forms using the XFA form template or XSD.
- Do not use unbound fields in bound repeatable panels.
As a rule of thumb, you should 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 XML schema or the XFA form template and add an entry for unbided fields, so that it also becomes bound and its data is available like other fields in the XML.
Adaptive forms can be prepopulated using the user data in XML format via the following protocols if configured with valid regex:
Passing authentication parameters is not supported.
You can also set the data attribute in slingRequest, where the data attribute is a string containing XML tags, as shown in the sample code below.
<% String dataXML="<afData>" + "<afUnboundData>" + "<dataRoot>" + "<first_name>"+ "Tyler" + "</first_name>" + "<last_name>"+ "Durden " + "</last_name>" + "<gender>"+ "Male" + "</gender>" + "<location>"+ "Texas" + "</location>" + "</dataRoot>" + "</afUnboundData>" + "</afData>"; slingRequest.setAttribute("data", dataXML); %>
You can write a simple XML 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.
Let's take an 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'd like to prepopulate 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.