MChan
MChan

Reputation: 7162

Meteor autoform Form hidden fields not rendering defaultValue and not saving

I have two hidden fields in my autoform Schema defined as shown below. I wish to save those two fields with other fields while not showing them to the app user. But I noticed from the autoform rendered html that the two hidden fields have no value, also they don't save with other fields to DB. Not sure what I might be missing / wrong here? Thanks for your help

  Invoice = new SimpleSchema({
    clientid: {
      type: String,
      optional: true
    },
    total: {
      type: String,
      label: 'Total Amount',
      optional: true
    },
    tax: {
      type: String,
      label: 'Taxes',
      optional: true
    },
    category: {
      type: String,              
      optional: true,
      autoform: {
        type: "hidden",                
        label: false
      },
      defaultValue: 'Test Category'
    }
  });

  {{> quickForm id="invoiceForm" buttonContent="Insert" buttonClasses="btn btn-primary btn-sm" schema=Invoice type="method" meteormethod="saveInvoice"}}

Upvotes: 3

Views: 994

Answers (3)

Mahmoud Metwally
Mahmoud Metwally

Reputation: 990

I don't think that you can have a field in your aldeed Schema that will render/ get saved in the form as a hidden field. So I suggest you pass the data (which you originally wanted to pass a a hidden field) though Sessions.

For example, if you are using autoform >> meteormethod to save the form, then you can save the session content within the server method. If you are not using a method, then you might want to pass the hidden data through Autoform.hooks >> onSubmit

Upvotes: 3

Thomas Goorden
Thomas Goorden

Reputation: 280

Have you tried not making the category field optional? There seems to be a conceptual problem between having a defaultValue and having the field as optional.

Upvotes: 0

Rafael Quintanilha
Rafael Quintanilha

Reputation: 1432

In my opinion it's best to keep the logic related to the form to the quickform template, for the case you would reuse your schema in another form, for instance.

I'd recommend you to do the following:

... 
},
category: {
   type: String,              
   optional: true,
   defaultValue: 'Test Category'
}
...

And use the omitFields clause (note you can specify multiple fields to be omitted separating them by comma):

{{> quickForm id="invoiceForm" buttonContent="Insert" buttonClasses="btn btn-primary btn-sm" schema=Invoice type="method" meteormethod="saveInvoice" omitFields="category, foo, bar, ..."}}

I've noted you are using method as the form type. If you are manually setting a method for saving your data, you might consider specify default and auto values inside the method itself. It will give you more freedom and control over your data.

Upvotes: 1

Related Questions