Nick Lehmann
Nick Lehmann

Reputation: 627

Meteor AutoForm stops proceeding submit

I would like to create a form, using the autoform package for Meteor, for my CAS_Entry collection. The code can be seen below. I also added the defined hooks, of which unfortunately only beginSubmit and before are executed and no entry is added to the collection. Using Meteor shell, the insert works like a charm.

I am grateful for any hint.

addCasEntry.html, Template for displaying the form:

{{#autoForm collection="CAS_Entry" type="insert" id="addCasEntryForm"}}
  {{> afQuickField name="type" options="allowed"}}
  {{> afQuickField name="description" rows="6" type="textarea"}}
  {{> afQuickField name="file" type="cfs-file" collection="Images"}}
  {{> afQuickField name="date" }}
  <button type="submit" class="btn btn-primary">Add</button>
{{/autoForm}}

addCasEntry.js, adding debugging hooks:

AutoForm.hooks({
  addCasEntryForm: {
    before: {
      insert: function(doc) {
        console.log(doc);
      }
    },
    after: {
      insert: function(error, result) {
        console.log('Occured error: ' + error);
      }
    },
    beginSubmit: function() {
      console.log('begin submit');  
    },
    onSuccess: function(formType, result) {
      console.log("Insert succeeded");
      console.log('Result ' + result);
    },
    onError: function(formType, error) {
      console.log('Error!!!');
      console.log(error);
    }
  }
});

SimpleSchema.debug = true;

/lib/collection/cas_entry.js:

CAS_Entry = new Mongo.Collection("cas_entries");

CAS_Entry.attachSchema(new SimpleSchema({
  type: {
    type: String,
    allowedValues: ['reflection', 'evidence']
  },
  description: {
    type: String,
    optional: true
  },
  file: {
    type: String,
    optional: true,
  },
  timeUploaded: {
    type: Date,
    optional: true,
    autoValue: function() {
      return new Date();
    }
  },
  date: {
    type: Date,
  }
}));

CAS_Entry.allow({
  'insert': function() {
    return true;
  },
  'update': function() {
    return true;
  }
});

And here is the console output:

console output

Upvotes: 0

Views: 280

Answers (1)

Matthias A. Eckhart
Matthias A. Eckhart

Reputation: 5156

Your form won't be submitted because you are not returning or passing the document to this.result(); inside your before hook.

AutoForm.hooks({
  addCasEntryForm: {
    // ...
    before: {
      insert: function(doc) {
        console.log(doc);
        return doc;
      }
    }
    // ...
  }
});

According to the documentation, you should use one of the following statements depending on your defined preconditions:

  • Synchronous, submit: return doc;.
  • Synchronous, cancel: return false;.
  • Asynchronous, submit: this.result(doc);.
  • Asynchronous, cancel: this.result(false);.

Upvotes: 1

Related Questions