Expand my Community achievements bar.

SOLVED

Pre-populate form fields

Avatar

Level 2

Hi, 

I have an adaptive form which has unbound and bound fields because it it partially based on a XDP form model. 

I now need to pre-fill some of the fields with data from the currently logged in user. 

I found Adobe example files and took the following route: 

  1. Created a prefill.jsp 
  2. Include the prefill.jsp in the head.jsp of the form
  3. prefill.jsp gets the user data and creates the xml:

<%
    final boolean isAnonymous = UserPropertiesUtil.isAnonymous(slingRequest);
    boolean isDisabled = WCMMode.fromRequest(request).equals(WCMMode.DISABLED);
    if(!isAnonymous && isDisabled && slingRequest.getParameter("dataRef")== null
            && slingRequest.getAttribute("data")==null){

        UserProperties userProperties = slingRequest.adaptTo(UserProperties.class);
        String givenName = userProperties.getProperty("givenName");
        String dataXML="<afData>" +
                            "<afBoundData>" +
                                "<dataRoot>" +
                                    "<General_firstName>"+ StringUtils.trimToEmpty(userProperties.getProperty("givenName")) + "<General_firstName>" +
                                    "</dataRoot>" +
                            "</afBoundData>" +
                        "</afData>";
        slingRequest.setAttribute("data", dataXML);
    }
%>

In the above snippet, "<General_firstName>" is the element name of the form field. 

The jsp is included and has no errors, but the field is not pre-filled. Does the XML need a different structure? The above field was dragged from the content finder (the loaded XDP) but I also have unbound fields. 

Any help is greatly appreciated. 

Please don't point me to the documentation -  https://helpx.adobe.com/aem-forms/6-1/prepopulate-adaptive-form-fields.html - since it did not help me :(

Thanks, Alex

1 Accepted Solution

Avatar

Correct answer by
Level 9

import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.UserManager;
import  org.apache.jackrabbit.api.security.user.Authorizable ;

i have used the following

try {
            UserManager um = ((JackrabbitSession) session).getUserManager();
            Authorizable loggedinUser = um.getAuthorizable(session.getUserID());
            System.out.println("The path of the user is"+loggedinUser.getPath());
            System.out.println("The family name is "+loggedinUser.getProperty("profile/familyName")[0].getString());

}

suggest you use prefill service to populate forms , instead of doing it in the JSP page
        

View solution in original post

7 Replies

Avatar

Level 3

Hi,

Can you throw some light on what was the problem and what were the steps taken to solve it ? Also, with 6.2, we have introduced a much simpler and manageable way to prefill forms with the notion of "prefill service", the documentation of this would be live soon.

Thanks,

Rishi

Avatar

Level 2

Hi Rishi, 

it needs to be <data> and not <dataRoot> as stated in the documentation. Submitting a form saves the form data in CRX, if you download the 'data' element you can look at the actual XML structure. Pre-filling needs to have the same structure.

Also, pre-filling can only be viewed in WCM Mode disabled, not in preview mode. 

Cheers, 

Alex

Avatar

Community Advisor

Hello Alex,

I am working on AEM forms too,

I also have tried to use prefilling of form by using data attribute but its not working.

Even tried the things you have suggested, still facing problem.

Please help me

Here is the data attribute:

<%
String dataXML="<afData>"+
                    "<afUnboundData>"+
                        "<data>"+
                            "<textbox1>hello</textbox1>"+
                            "<textbox2>hii</textbox2>"+
                        "</data>"+
                    "</afUnboundData>"+
                "</afData>";
slingRequest.setAttribute("data", dataXML);
%>

The Form is without any data model and containing only two textbox with name "textbox1" and "textbox2"

Avatar

Employee Advisor

try the following steps first

1.Set the form's submit action to "Store Content". Preview the form and enter some value in the text fields

2.Submit the form

3.go to the node where the form's data is submitted. Get the data xml from the jcr:data node. Save the data xml as a xml file on your file system

4.Preview you form with the data xml stored in the previous steps

5.The forms should prepopulate with the data from the xml file

6.Then make sure the data that you are passing in your code matches the structure in the xml file which you got in step 4

Avatar

Community Advisor

Hello Alex,

I have already done so,

The xml i got was

<?xml version="1.0" encoding="UTF-8"?><afData>
  <afUnboundData>
    <data>
      <textbox1>hgbfvd</textbox1>
      <textbox2>ujhytgrfed</textbox2>
    </data>
  </afUnboundData>
  <afBoundData>
    <data xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"/>
  </afBoundData>
  <afSubmissionInfo>
    <lastFocusItem>guide[0].guide1[0].guideRootPanel[0].textbox2[0]</lastFocusItem>
    <computedMetaInfo/>
  </afSubmissionInfo>
</afData>

 

taking it as a base i set the data attribute , just removed unnecessary tags from above xml

Avatar

Correct answer by
Level 9

import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.UserManager;
import  org.apache.jackrabbit.api.security.user.Authorizable ;

i have used the following

try {
            UserManager um = ((JackrabbitSession) session).getUserManager();
            Authorizable loggedinUser = um.getAuthorizable(session.getUserID());
            System.out.println("The path of the user is"+loggedinUser.getPath());
            System.out.println("The family name is "+loggedinUser.getProperty("profile/familyName")[0].getString());

}

suggest you use prefill service to populate forms , instead of doing it in the JSP page