Chapter 5. $fh.forms

To use $fh.forms, you must initialize it using $fh.forms.init. However, to use $fh.forms.init, you must call $fh.init.

Important

$fh.init must complete before using $fh.forms.init. For more information, see fh-init.

Supported Platforms

  • JavaScript SDK

    • Cordova
    • Web Apps

For detailed version information, see Supported Configurations.

5.1. SDK Structure

5.1.1. Core

Core Structure

5.2. $fh.forms.init

$fh.forms.init(params, callback);

5.2.1. Details

Initialise appForm SDK. Currently the params is not used so pass in empty JSON object. callback will be called when initialisation finished.

5.2.2. Example

var params = {};
$fh.forms.init(params, function(err) {
  if (err) console.error(err);

  // Forms initialised ok if no error
});

5.3. $fh.forms.getForms

$fh.forms.getForms(params, callback);

5.3.1. Details

Retrieves an array of Form models. Can be loaded from the server if the 'fromRemote' parameter is set to true, or from local storage if set to false.

5.3.2. Example

var params = {
 "fromRemote": true
};

$fh.forms.getForms(params, function(err, forms){
  if(err) console.error(err);

  // forms is an instance of $fh.forms.models.forms
  // See Models section for details of its API
  // for example, getFormsList() returns an array of Forms models

  var formsList = forms.getFormsList();
  console.log(formsList);
});

5.4. $fh.forms.getForm

$fh.forms.getForm(params, callback);

5.4.1. Details

Retrieves a form based on a specified ID.

5.4.2. Example

var params = {
 "fromRemote" : true,
 "formId" : "1234"
};

$fh.forms.getForm(params, function(err, form){
if(err) console.error(err);

var formName = form.getName();
var formDesc = form.getDescription();
console.log('Form Name: ', formName, 'Form Desc: ', formDesc);
});

5.5. $fh.forms.getTheme

$fh.forms.getTheme(params, callback);

5.5.1. Details

Retrieves a theme. If the 'css' parameter is set to true, css is returned. If it is set to false, a Theme object is returned.

5.5.2. Example

var params = {
 "css" : true //if set to true, returns css.
};

$fh.forms.getTheme(params, function(err, theme){
if(err) console.error(err);

console.log(theme);
});

5.6. $fh.forms.getSubmissions

$fh.forms.getSubmissions(options, callback);

5.6.1. Details

Returns a list of submissions that have been successfully completed.

5.6.2. Example

var params = {};

$fh.forms.getSubmissions(params, function (err, submissions) {
  if (err) console.error(err);

  console.log('Array of completed submissions', submissions);
});

5.7. $fh.forms.downloadSubmission

5.7.1. Details

Triggers the download of a submission.

There are two forms of this API.

5.7.2. Example: Callback Passed

In this example, passing a callback to the $fh.forms.downloadSubmission function means that the callback will only be called when the submission, including all files, has fully completed downloading.

var params = {
  'submissionId': "<< ID of the submission stored in the cloud >>"
};

//Downloading the submission.
//If the submission has already been downloaded to local memory, it will be loaded from there instead of downloading from the cloud.
$fh.forms.downloadSubmission(params, function(err, downloadedSubmission){
  if(err){
    return console.error(err);
  }

  //The form that the submission was submitted against is sent back with the submission from the cloud.
  var formSubmittedAgainst = downloadedSubmission.getFormSubmittedAgainst();
  var formId = downloadedSubmission.getFormId();
});

5.7.3. Example: Callback Not Passed

In this example, not passing a callback will queue the submission for download. Global event listeners can then be used to monitor the download progress.

var params = {
  'submissionId': "<< ID of the submission stored in the cloud >>"
};

//A global event listener for all submission 'downloaded' events
$fh.forms.on('submission:downloaded', function(remoteSubmissionId){
  //The form that the submission was submitted against is sent back with the submission from the cloud.
  var formSubmittedAgainst = this.getFormSubmittedAgainst();
  var formId = this.getFormId();
});

//A global event listener for all submission 'error' events
$fh.forms.on('submission:error', function(errorMessage){
  var localId = this.getLocalId();

  console.error(errorMessage + " for submission with local ID " + localId);
});

//Downloading the submission.
//If the submission has already been downloaded to local memory, it will be loaded from there instead of downloading from the cloud.
$fh.forms.downloadSubmission(params);

5.8. $fh.forms.log

$fh.forms.log.e(message); //logs an error
$fh.forms.log.w(message); //logs a warning
$fh.forms.log.d(message); //logs a debug message
$fh.forms.log.l(message); //logs success

5.8.1. Details

There are 4 types of logs:

  • Error: $fh.forms.log.e('error');
  • Warning: $fh.forms.log.w('warning');
  • Debug: $fh.forms.log.d('debug');
  • Log: $fh.forms.log.l('successful log');

5.8.2. Example

var params = {
 'fromRemote' : true
};

$fh.forms.getForms(params, function(err, formsList){
if(error) $fh.forms.log.e(err); //log error

console.log('Lit of forms: ', formsList.getFormsList());
  $fh.forms.log.l('Forms loaded successfully'); //log successfuly loading of forms
});

5.9. $fh.forms.getLogs

$fh.forms.log.getLogs();

5.9.1. Details

Returns log information.

$fh.forms.getSubmissions({}, function (err, submissions) {
  if (err) {
    console.log('Error loading submissions', err);
  } else {
    $fh.forms.log.l('Array loaded successfully') //Recorded log
    console.log('Array of completed submissions', submissions);


    var logRecords = $fh.forms.log.getLogs();
    console.log('Log Record', logRecords); //Prints out an array of all logs
  }
});

5.10. Global Events

5.10.1. Details

The $fh.forms API provides a global event emitter for all events that occur in the $fh.forms API.

The event name is defined in the format model:eventname. For example, if the Submission model emits the submitted event, the global event name would be submission:submitted. This event would fire whenever any submissions has been submitted and uploaded.

5.10.2. $fh.forms.on

$fh.forms.on("submission:progress", function(progressJSON) {
    //See Submission Model Progress For progressJSON Definition
});

5.10.3. $fh.forms.once

$fh.forms.once("submission:submitted", function(submissionId) {
    //Note: this refers to the individual submission that emitted the submitted event.
    assert.equal(this.getRemoteSubmissionId(), submissionId);
});

5.11. $fh.forms.models.forms

5.11.1. Details

This is a list of Form models returned by $fh.forms.getForms(). The functions listed below can be called on the returned Form Models array.

5.11.2. $fh.forms.models.forms.clearAllForms

$fh.forms.models.forms.clearAllForms(cb);

5.11.2.1. Details

Clear all local forms.

Note

This function is currently being implemented

5.11.2.2. Example

formsList.clearAllForms(function(err) {
    if (err) {
        console.log('Error deleting forms', err);
    } else {
        console.log('Reloading forms list', formsList.getFormsList()); //will return empty list
    }
});

5.11.3. $fh.forms.models.forms.isFormUpdated

$fh.forms.models.forms.isFormUpdated(formModel);

5.11.3.1. Details

Check if a specific form model is up to date.

5.11.3.2. Example

var model = new $fh.forms.models.Form();
var updated = $fh.forms.models.forms.isFormUpdated(model);
console.log(updated);

5.11.4. $fh.forms.models.forms.getFormMetaById

$fh.forms.models.forms.getFormMetaById(formId)

5.11.4.1. Details

Get form meta object by formId.

5.11.4.2. Example

var exampleFormId = '1234';

var params = {
 'fromRemote' : true
};

$fh.forms.getForms(params, function(err, formsList){
if(err) console.error(err);

var formDetails = formsList.getFormMetaById(exampleFormId); //gets meta object from forms list based on id
console.log(formDetails);
});

5.11.5. $fh.forms.models.forms.refresh

$fh.forms.models.forms.refresh(fromRemote, cb);

5.11.5.1. Details

Read form list model from local or remote forcely. It will store to local storage automatically if it does not exist.

5.11.5.2. Example

formsList.refresh(true, function (err) { //if fromRemote == true, forms are read from server.
// If false, reads from local storage
  if (err) {
    console.log('Error refreshing form', err);
  } else {
    console.log('Refreshed form list', formsList); //prints newly refrehsed list
  }
});

5.11.6. $fh.forms.models.forms.clearLocal

$fh.forms.models.forms.clearLocal(cb);

5.11.6.1. Details

Removes model from local storage but not from RAM.

5.11.6.2. Example

formsList.clearLocal(function (err) {
  if (err) {
    console.log('Error occurred clearing forms from local storage', err);
  } else {
    console.log('Reloading forms', formsList.getFormsList());
  }
});

5.11.7. $fh.forms.models.forms.getFormsList

$fh.forms.models.forms.getFormsList();

5.11.7.1. Details

Retrieve an array containing forms meta data.

5.11.7.2. Example

var params = {
  'fromRemote': true
};

$fh.forms.getForms(params, function (err, formsList) {
  if (err) console.error(err);

  var forms = formsList.getFormsList();
  console.log(forms);
});

5.11.8. $fh.forms.models.forms.size

$fh.forms.models.forms.size();

5.11.8.1. Details

Retrieve the number of forms stored.

5.11.8.2. Example

var params = {
  "fromRemote": true
};

$fh.forms.getForms(params, function (err, formsList) {
  if (err) console.error.(err);

  var numOfForms = formsList.size();
  console.log(numOfForms);
});

5.12. $fh.forms.models.Form

5.12.1. Details

This is a Form model. A list of Form models is returned by calling forms.getFormsList().

5.12.2. $fh.forms.models.Form.constructor()

$fh.forms.models.Form(params,cb);

5.12.2.1. Details

Construct a Form object. Callback when form definition is loaded.

5.12.2.2. Example

var params = {
  "formId": ”1234”,
  "fromRemote": true
};

$fh.forms.getForm(params, function (err, form) {
  if (err) console.error(err);

  // new form model
  console.log(form);
});

5.12.3. $fh.forms.models.Form.getLastUpdate()

form.getLastUpdate();

5.12.3.1. Details

Retrieve last updated timestamp on server.

5.12.3.2. Example

var params = {
  "formId": ”1234”,
  "fromRemote": true
};

$fh.forms.getForm(params, function (err, form) {
  if (err) console.error(err);

  var lastUpdate = form.getLastUpdate();
  console.log(lastUpdate);
});

5.12.4. $fh.forms.models.Form.getPageModelList()

form.getPageModelList();

5.12.4.1. Details

Retrieve an array of page models associated to this form.

5.12.4.2. Example

var params = {
  "formId": "1234",
  "fromRemote": true
};

$fh.forms.getForm(params, function (err, form) {
  if (err) console.error(err);

  var pageList = form.getPageModelList();
  console.log('Array of pages associated with this form', pageList);
});

5.12.4.3. fh.forms.models.Form.getRuleEngine()

form.getRuleEngine();

5.12.4.4. Details

Retrieve rule engine attached to the form().

5.12.4.5. Example

var params = {
  "formId": 1234,
  "fromRemote": true
};

$fh.forms.getForm(params, function (err, form) {
  if (err) console.error(err);

  var ruleEngine = form.getRuleEngine();
  console.log(ruleEngine);
});

5.12.5. $fh.forms.models.Form.getName()

form.getName();

5.12.5.1. Details

Retrieve form name.

5.12.5.2. Example

var params = {
  "formId": "1234",
  "fromRemote": true
};

$fh.forms.getForm(params, function (err, form) {
  if (err) console.error(err);

  var formName = form.getName();
  console.log(formName);
});

5.12.6. $fh.forms.models.Form.getDescription()

form.getDescription();

5.12.6.1. Details

Retrieve form description.

5.12.6.2. Example

var params = {
  "formId": "1234",
  "fromRemote": true
};

$fh.forms.getForm(params, function (err, form) {
  if (err) console.error(err);

  var formDescription = form.getDescription();
  console.log(formDescription);
});

5.12.7. $fh.forms.models.Form.getFormId()

form.getFormId();

5.12.7.1. Details

Retrieve form Id.

5.12.7.2. Example

var params = {
  "formId": "1234",
  "fromRemote": true
};

$fh.forms.getForm(params, function (err, form) {
  if (err) console.error(err);

  var formId = form.getId();
  console.log(formId);
});

5.12.8. $fh.forms.models.Form.getFieldModelById()

form.getFieldModelById(fieldId);

5.12.8.1. Details

Retrieve field model by field id.

5.12.8.2. Example

var params = {
  "formId": "1234",
  "fromRemote": true,
  "fieldId": "123"
};

$fh.forms.getForm(params, function (err, form) {
  if (err) console.error(err);

  var fieldModel = form.getFieldModelById(fieldId);
  console.log(fieldModel);
});

5.12.9. $fh.forms.models.Form.newSubmission()

form.newSubmission();

5.12.9.1. Details

Initialise a new submission model for this form.

5.12.9.2. Example

form.newSubmission(); //creates a new submission

5.12.10. $fh.forms.models.Form.removeFromCache()

form.removeFromCache();

5.12.10.1. Details

Remove current form model from memory cache (singleton).

5.12.10.2. Example

form.removeFromCache();

5.12.11. $fh.forms.models.Form.refresh()

form.refresh([fromRemote], cb);

5.12.11.1. Details

Read form model from local memory or remote forcely. It will sotre to local storage automatically if it does not exist.

5.12.11.2. Example

form.refresh(true, function (err) {
  if (err) {
    console.log('Error refreshing', err);
  } else {
    console.log('Refreshed page');
    //refresh successful
  }
});

5.12.12. $fh.forms.models.Form.clearLocal()

form.clearLocal(cb)

5.12.12.1. Details

Remove locally stored Form.

5.12.12.2. Example

foundForm.clearLocal(function (err) {
  if (err) {
   console.log('Error removing form');
  } else {
   //form cleared successfully
  }
});

5.13. $fh.forms.models.Page

5.13.1. Details

Listed below are the functions that can be called on a Page model.

5.13.2. $fh.forms.models.Page.setVisible()

page.setVisible(isVisible);

5.13.2.1. Details

Set if this page model should be visible or not. Will emit ‘visible’ or ‘hidden’ based on whether the boolean value 'isVisible' is set to true or false.

5.13.2.2. Example

page.setVisible(true) //Boolean value to determine whether page is set to visible or not.

5.13.3. $fh.forms.models.Page.getName()

page.getName();

5.13.3.1. Details

Retrieves page name.

5.13.3.2. Example

 var pageList = foundForm.getPageModelList(); //Iterates through all pages of a returned form and prints out page names
 for (var page = 0; page < pageList.length; page++) {
   var currentPage = pageList[page];
   console.log('Name of current page is: ', currentPage.getName());
 }

5.13.4. $fh.forms.models.Page.getDescription()

page.getDescription();

5.13.4.1. Description

Retrieve page description.

5.13.4.2. Example

page.getDescription();

5.13.5. $fh.forms.models.Page.getFieldModelList()

page.getFieldModelList();

5.13.5.1. Details

Retrieve field models associated to this page.

5.13.5.2. Example

var pageList = form.getPageModelList(); //Retrieves all pages of a form

for (var page = 0; page < pageList.length; page++) { //Iterates through all pages
  var currentPage = pageList[page];
  var pageFields = currentPage.getFieldModelList(); //Retrieves all fields on a page
  console.log(pageFields); //Lists all fields
}

5.13.6. $fh.forms.models.Page.getFieldModelById()

page.getFieldModelById(fieldId);

5.13.6.1. Details

Retrieve a specific field model. The field model does not need to be in this page. Alias of Form.getFieldModelById(fieldId).

5.13.6.2. Example

var fieldId = '1234';

page.getFieldModelById(fieldId); //Returns Field model

5.14. $fh.forms.models.Field

5.14.1. Details

A list of Field objects can be returned by calling the getFieldModelList() function on a Page model.

pageOne.getFieldModelList();

Listed below are a set of functions that can be called to access various attributes of a Field model.

5.14.2. $fh.forms.models.Field.isRequired()

currentField.isRequired();

5.14.2.1. Details

Returns true if the field is a required field.

5.14.2.2. Example

currentField.isRequired(); //will return true if the field is required

5.14.3. $fh.forms.models.Field.isRepeating()

field.isRepeating();

5.14.3.1. Details

Returns true if the field is a repeating field, or false if the field is not a repeating field.

5.14.3.2. Example

field.isRepeating();

5.14.4. $fh.forms.models.Field.getType()

field.getType();

5.14.4.1. Details

Returns the type of the field.

5.14.4.2. Example

field.getType();

5.14.5. $fh.forms.models.Field.getName()

field.getName();

5.14.5.1. Details

Returns the name of the field.

console.log(field.getName()); //pritns name of field

5.14.6. $fh.forms.models.Field.getCode()

field.getCode();

5.14.6.1. Details

Returns the Field Code for a field if it exists. If the field was not assigned a field code in the Studio null is returned.

5.14.7. $fh.forms.models.Field.getHelpText()

field.getHelpText();

5.14.7.1. Details

Returns field instruction text.

5.14.7.2. Example

field.getHelpText();

5.14.8. $fh.forms.models.Field.validate(inputValue)

field.validate(inputValue, callback(err,res))

5.14.8.1. Details

Returns the inputValue object if validation is successful, or an error message if validation fails.

5.14.8.2. Example

var field = form.getFieldModelById(fieldId);
var inputValue = elem.value; //Value of an element such as text field, numberfield etc

field.validate(inputValue, function (err, res) {
  if (err) {
    console.log('Validation Error', err);
  } else {
    console.log('Validation Successful', res);
  }
});

5.14.9. $fh.forms.models.Field.getRules()

field.getRules();

5.14.9.1. Details

Returns an array of rule objects that are associated with the field.

5.14.9.2. Example

field.getRules();

5.14.10. $fh.forms.models.Field.getCheckboxOptions()

field.getCheckboxOptions();

5.14.10.1. Details

Retuns an array of check box choices.

Note

Only valid for check boxes field

5.14.10.2. Example

if (field.getType() == 'checkboxes') {
  console.log('Checkbox options: ', field.getCheckBoxOptions());
}

5.14.11. $fh.forms.models.Field.getRadioOption()

field.getRadioOption();

5.14.11.1. Details

Returns radio box options.

Note

Only valid for radio field

5.14.11.2. Example

if (field.getType() == 'radio') {
  console.log('Radio options: ', field.getRadioOption());
}

5.15. $fh.forms.models.submissions

5.15.1. $fh.forms.models.submissions.getSubmissions()

$fh.forms.models.submissions.getSubmissions();

5.15.1.1. Details

Returns a list of submission meta data.

5.15.1.2. Example

submissions.getSubmissions();

5.15.2. $fh.forms.models.submissions.getSubmissionMetaList()

submissions.getSubmissionMetaList();

5.15.2.1. Details

Returns a list of submission meta data

5.15.2.2. Example

submissions.getSubmissionMetaList();

5.15.3. $fh.forms.models.submissions.findByFormId(formId)

submissions.findByFormId(formId);

5.15.3.1. Details

Retrieves the meta data for specified form.

5.15.3.2. Example

var formId = '53146bf95a133733451cd35b';

$fh.forms.getSubmissions({}, function (err, submissions) {
  if (err) {
    console.log('Error loading submissions', err);
  } else {
    console.log('Array of completed submissions', submissions);
    var foundSubmissions = submissions.findByFormId(formId);
    console.log('Array of submissions for specified form: ', foundSubmissions);

  }
});

5.15.4. $fh.forms.models.submissions.clear(cb)

sumissions.clear(function(err))

5.15.4.1. Details

Clear submission meta list from this model and local storage.

5.15.4.2. Example

$fh.forms.getSubmissions({}, function(err, submissions) {
    if (err) {
        console.log('Error loading submissions', err);
    } else {
        console.log('Array of completed submissions', submissions);
        submissions.clear(function(err) {
            if (err) console.err(err);
        });
    }
});

5.15.5. $fh.forms.models.submissions.getDrafts()

submissions.getDrafts()

5.15.5.1. Details

Return submission drafts()

5.15.5.2. Example

submissions.getDrafts();

5.15.6. $fh.forms.models.submissions.getPending()

submissions.getPending()

5.15.6.1. Details

Returns pending submissions.

5.15.6.2. Example

submissions.getPending();

5.15.7. $fh.forms.models.submissions.getSubmitted()

submissions.getSubmitted()

5.15.7.1. Details

Returns submitted submissions.

5.15.7.2. Example

submissions.getSubmitted();

5.15.8. $fh.forms.models.submissions.getError()

submissions.getError()

5.15.8.1. Details

Returns submissions that have errors.

5.15.8.2. Example

submissions.getError();

5.15.9. $fh.forms.models.submissions.getInProgress()

submissions.getInProgress()

5.15.9.1. Details

Return submissions that are currently in progress.

5.15.9.2. Example

submissions.getInProgress();

5.15.10. $fh.forms.models.submissions.getSubmissionByMeta(meta,cb)

submissions.getSubmissionByMeta(meta, function(err, res))

5.15.10.1. Details

Retrieves a submission model object by submission meta data from submission list model.

5.15.10.2. Example

var params = submissions.getSubmitted()[0];

submissions.getSubmissionByMeta(params, function (err, submission){
  if(err) console.error(err);

  console.log('Returned Submission',submission);
});

5.15.11. $fh.forms.models.submissions.getSubmissionByLocalId(localId,cb)

submissions.getSubmissionByLocalId(localId, function(err, submissionModel){})

5.15.11.1. Details

Retrieves a submission model by the Local ID of the submission.

5.15.11.2. Example

var localId = "sublocalid1";

submissions.getSubmissionByLocalId(localId, function (err, submission){
  if(err) console.error(err);

  console.log('Returned Submission',submission);
});

5.15.12. $fh.forms.models.submissions.getSubmissionByRemoteId(remoteId,cb)

submissions.getSubmissionByRemoteId(remoteId, function(err, submissionModel){})

5.15.12.1. Details

Retrieves a submission model by the Remote ID of the submission.

5.15.12.2. Example

var remoteId = "subremoteid1";

submissions.getSubmissionByRemoteId(remoteId, function (err, submission){
  if(err) console.error(err);

  console.log('Returned Submission',submission);
}

5.16. $fh.forms.models.Submission

5.16.1. Details

Submission models contaon user input and related meta information. A list of Submission models can be returned by calling the .getSubmissions() function on a list of submissions.

submissions.getSubmissions(); //double check

Listed below are functions that can be called on Submission models.

5.16.2. $fh.forms.models.Submission.saveDraft(cb)

submission.saveDraft(cb);

5.16.2.1. Details

Save current submission to draft / local storage.

5.16.2.2. Example

currentSubmission.saveDraft(function (err) {
  if (err) {
    console.log(err);
  } else {
    console.log('Draft saved to local storage');
  }
});

5.16.3. $fh.forms.models.Submission.submit()

submission.submit(cb)

5.16.3.1. Details

Submit current submission. It will create a task for uploading.

5.16.3.2. Example

currentSubmission.submit(function (err) {
  console.log(!err);
  if (err) {
    console.log(err);
  }
});

currentSubmission.on("submit", function () {
 /* Upload the submission. */
  currentSubmission.upload(function (err) {
    if (err) {
      console.log(err);
    }
  });
});

5.16.4. $fh.forms.models.Submission.getStatus()

submission.getStatus();

5.16.4.1. Details

Returns the current status of the submission.

5.16.4.2. Example

submission.getStatus();

5.16.5. $fh.forms.models.Submission.addComment()

submission.addComment(message, [username]);

5.16.5.1. Details

Allows a user to add a comment to the current submission.

5.16.5.2. Example

submission.addComment('test message', 'test user');

5.16.6. $fh.forms.models.Submission.getComments()

submission.getComments()

5.16.6.1. Details

Returns an array of commetns for the current submission.

5.16.6.2. Example

submission.addComment('test message', 'test user');

console.log(submission.getComments()); //will return an array containing the above comment

5.16.7. $fh.forms.models.Submission.removeComment(timeStamp)

submission.removeComment(timeStamp);

5.16.7.1. Details

Remove a comment from the current submission via its timestamp.

5.16.7.2. Example

submission.removeComment(timeStamp);

5.16.8. $fh.forms.models.Submission.addInputValue(params,cb)

$fh.forms.models.Submission(params, callback(err, res))

5.16.8.1. Details

Add a value to submission for a field. This will validate the input value and it will return an error message as a string if failed. If in transaction mode, it will not immediately add user input value to submission but a temp variable. If not in transaction mode, the input value is added to submission immediately. If the "sectionIndex" parameter is provided, a new value is added to the field in the given section index, within the repeating section group.

5.16.8.2. Example

var params = {
    "fieldId": '53146c1f04e694ec1ad715b6',
    "value": 'New example text',
  "Index":optional,
    "sectionIndex":optional

};
currentSubmission.addInputValue(params, function(err, res) {
    if (err) console.error(err);

    console.log('Newly added input: ', res);
});

5.16.9. $fh.forms.models.Submission.startInputTransaction()

submission.startInputTransaction()

5.16.9.1. Details

Start a transaction for user input. if already started, it will drop temporary input.

5.16.9.2. Example

submission.startInputTransaction();

5.16.10. $fh.forms.models.Submission.endInputTransaction(isSucceed)

$fh.forms.models.Submission.endInputTransaction(isSucceed) //'isSucceed' is a boolean value.

5.16.10.1. Details

End the transaction. If the transaction succeeds, it will copy temporary input to submission to be uploaded. If it fails, it will drop the temporary user input. 'isSucceed' is a boolean value. If 'true' is passed into the function, the transaction is complete and the user input is transfered from temporary storage to the submission. If 'false' is passed into the function, then the temporary values are discarded and the transaction is not completed.

5.16.10.2. Example

submission.startInputTransaction();

var params = {
    "fieldId": '53146c1f04e694ec1ad715b6',
    "value": 'Example text'
  };

submission.addInputValue(params, function(err, res) {
    if (err) {
        console.log('Error adding input', err);
        submission.endInputTransaction(false); //Transaction failed. New values are not added to submission.
    } else {
        console.log('Updated value: ', res);
        submission.endInputTransaction(true); //End input transaction. New value is added to submission.
    }
});

5.16.11. $fh.forms.models.Submission.reset()

submission.reset();

5.16.11.1. Details

Remove all user input from this submission.

5.16.11.2. Example

submission.reset();

5.16.12. $fh.forms.models.Submission.getForm(cb)

submission.getForm(function (err, form))

5.16.12.1. Details

Returns Form object associated with the submission.

5.16.12.2. Example

submission.getForm(function(err, form) {
    if (err) console.error(err);

    console.log('Form associated with submission: ', form);
});

5.16.13. $fh.forms.Submission.removeFieldValue(fieldId, [index])

submission.removeFieldValue(fieldId, index, sectionIndex)
//Index is only specified when referencing repeated fields
//sectionIndex is only specified when referencing repeating section

5.16.13.1. Details

Remove a value from a specific field based on ID, sectionIndex may be passed to get value of a field in specific section, otherwise it defaults to 0.

5.16.13.2. Example

var exampleFieldId = '1234';

submission.removeFieldValue(exampleFieldId);

5.16.14. $fh.forms.Submission.getInputValueByFieldId(field,cb)

submission.getInputValueByFieldId(field, sectionIndex, function(err, res))

5.16.14.1. Details

Get input values associated with a field, sectionIndex may be passed to get value of a field in specific section, otherwise it defaults to 0.

var fieldId = '1234';

currentSubmission.getInputValueByFieldId(fieldId, function(err, res) {
    if (err) console.error(err);

    console.log('Field value: ', res);
});

5.16.15. Submission Events

The Submission Model emits events as they move through the submission process. Whenever a function is executed when an event is fired, the this object will always refer to the Submission object that emitted the event.

5.16.15.1. inprogress The Submission Is In The Process Of Uploading.

submission.on('inprogress', function(uploadTask) {
    var self = this;
    uploadTask.submissionModel(function(err, submissionModel) {
        //The 'this' parameter in the event refers the submission model that emitted the event.
        assert.strictEqual(self, submissionModel);
    });
});

5.16.15.2. error: There Was An Error Uploading The Submission.

submission.on('error', function(errorMessage) {
    console.error(errorMessage);
    assert.equal(errorMessage, this.getErrorMessage);
});

5.16.15.3. savedraft: The Submission Was Saved As A Draft

submission.on('savedraft', function() {
    //This is the local ID of the submission that emitted the 'savedraft' event.
    assert.isString(this.getLocalId());
});

5.16.15.4. validationerror: There Was A Validation Error When Making A Submission.

This error is only emitted locally. This is not a validation error on the server side.

submission.on('validationerror', function(validationObject) {
    //This is the local ID of the submission that emitted the 'savedraft' event.
    assert.isString(false, validationObject.valid);
});

Validation Object

{
    valid: < true / false > ,
    < fieldid1 > : {
        valid: < true / false > ,
        errorMessages: [
            "Validation Error Message 1",
            "Validation Error Message 2"
        ]
    },
    ....,
    < fieldidN > : {
        valid: < true / false > ,
        errorMessages: [
            "Validation Error Message 1",
            "Validation Error Message 2"
        ]
    }
}

5.16.15.5. submit: The Submission Is Valid And Can Now Be Uploaded.

The submission has passed local validation. It is now ready to be uploaded to the Cloud.

submission.on('submit', function() {
    //Valid Submission, it can be uploaded now or at any other time
    this.upload(function(err, uploadTask) {
            ...
    });
});

5.16.15.6. submitted: The Submission Is Valid And Has Completed Uploading All Data.

submission.on('submitted', function(remoteSubmissionId) {
    assert.equal(remoteSubmissionId, this.getRemoteSubmissionId());
});

5.16.15.7. queued: The Submission JSON Definition Has Been Uploaded. Proceeding To Upload Any Files.

At this point, the remote submission ID has been assigned on the Cloud side. The submission can now be considered valid on both Client and Cloud sides.

submission.on('queued', function(remoteSubmissionId) {
    assert.equal(remoteSubmissionId, this.getRemoteSubmissionId());
});

5.16.15.8. progress: The Progress For A Submission Has Been Incremented.

submission.on('progress', function(progressJSON) {
    //The Current Progress Of The Submission
});
5.16.15.8.1. progressJSON
{
    'formJSON': false, //Boolean specifying if the submission JSON has been uploaded.
    'currentFileIndex': 0,
    'totalFiles': 3,
    'totalSize': 54321, //Size in Bytes of the entire submission
    'uploaded': 12345, //Size, in Bytes already uploaded
    'retryAttempts': 1, //Number of times the submission has been tried.
    'submissionTransferType': < upload / download > , //Is the submission being uploaded or downloaded.
    'submissionRemoteId': "remoteSubmissionID1234", //The remote submission ID if it is available.
    'submissionLocalId': "localSubmissionID1234" //The local submission ID
}

5.16.15.9. downloaded: The Submission Has Completed Downloading (Only Used When Downloading Submissions.)

This also includes all files when a submission is downloaded to a mobile client device.

submission.on('downloaded', function(remoteSubmissionId) {
    assert.equal(remoteSubmissionId, this.getRemoteSubmissionId());
});