Skip to content

Pre fill form with data from the previous submission

Adam Becker edited this page Mar 26, 2015 · 2 revisions

use with FormRenderer 0.4.x

var PrefillFormRenderer = FormRenderer.extend({
  // Restore existing values
  initResponseFields: function(){
    FormRenderer.prototype.initResponseFields.call(this, arguments);

    var savedValues = store.get('savedValues');
    if (savedValues) {
      var self = this;
      _.each(savedValues, function(v, k){
        var rf = self.response_fields.get(k);
        if (!rf.getValue()) {
          rf.setExistingValue(v);
        }
      });
    }
  }
});

var fr = new PrefillFormRenderer({
  "project_id": 846 // Replace with your project ID, obviously :)
});

// Save existing values
fr.state.on('change:submitting', function(model, val){
  if(val){
    // Use IDs of fields that you want to remember
    var pickedValues = _.pick(fr.getValue(), '10504', '10505');
    store.set('savedValues', pickedValues);
  }
});