Working with PrizmDoc > Developer Guide > PrizmDoc Server > How To > Pre-Populate Fields in the E-Signature Viewer |
This example is to automatically pre-populate pre-existing user data into form fields at runtime. Route the form definition GET request to a custom handler instead of Prizm Application Services (PAS) directly. In this handler, fetch the Form Definition from PAS, insert default values into the given form definition using external data, and return the modified form definition to the client. This example assumes PAS is listening on localhost:3000, but this will vary based on your server configuration.
Example |
Copy Code
|
---|---|
GET http://localhost:3000/FormDefinitions/5418c96283bc469783bd30e7c8fdc059 Content-Type: application/json { "templateDocumentId": "Form 3.pdf", "globalSettings": { ... global settings ... }, "formRoles": { ... form roles ... }, "groups": {}, "formName": "Form 1 - updated", "formData": [ ... form data ... ] } |
This will vary based on your data source. You might load data from a database, a file, or another location. Or, your GET request to load the form definition may have included a session ID for a particular user from which secondary information can be queried. Let’s assume we receive the following user data object:
Example |
Copy Code
|
---|---|
{ "Name": "John Smith", "Address": "123 Town St", "Phone": "(555)555-5555" } |
We can iterate through each field in the formDefinition, check if there is data corresponding to that field ID in the example user data object, and set its defaultValue property appropriately depending on the field template type:
Example |
Copy Code
|
---|---|
//userData is the result of our example external data GET request //formDefinition is the result of our PrizmDoc FormData GET request formDefinition.formData = formDefinition.formData.map(function(field) { if (userData[field.fieldId]) { switch (field.template) { // Checkboxes are either "checked" or not case 'CheckboxTemplate': return extend({}, field, { defaultValue: userData[field.fieldId] ? 'checked' : '' }); // Signatures use a different value based on their type, // but we will assume text for this example case 'SignatureTemplate': case 'InitialsTemplate': return extend({}, field, { defaultValue: { type: 'text', value: userData[field.fieldId] } }); // Date templates use an ISO datetime case 'DateTemplate': return extend({}, field, { defaultValue: (new Date(userData[field.fieldId])).toISOString() }); // Text templates use a string case 'TextTemplate': return extend({}, field, { defaultValue: userData[field.fieldId] }); } } // If the item was not in the database, return it as-is. return field; }); |
A field collection has the option to "Allow Multiple Selections". If "Allow Multiple Selections" is false, but multiple fields in that group are set to pre-populate as "checked", the first field with a defaultValue of "checked" will be set as the only selected field for that collection. |
Return the updated formDefinition object to the function, web service, or other source that called it. When the data reaches the client and the FormLoaded event in the E-Signature Viewer fires, fields in this form definition with a valid defaultValue will be populated.