Nyxynyx
Nyxynyx

Reputation: 63599

Setting value for Omitted Field in Autoform in Meteor.js

When using a quickform from the aldeed:autoform package, how can we set the value of a field that we have omitted using omitFields? That field is omitted because we do not want the user to change its default value (eg: changing userId from Meteor.userId()) nor see that field.

Example:

{{> quickForm collection="Contacts" id="contacts-new-form" type="insert" omitFields="avatarUrl,details.active" buttonContent="Create Contact"}}

Upvotes: 0

Views: 3193

Answers (2)

Anus Ahmad
Anus Ahmad

Reputation: 1

@Nyxynyx You need to add this code. just use hook and add userId using before insertion. Look at the below example i hope this will help you.

var postHooks = {
  before: {
    insert: function(doc) {
      if(Meteor.userId()){
        doc.userId = Meteor.userId();
      }
      
      return doc;
    }
  }
}

Upvotes: 0

Serkan Durusoy
Serkan Durusoy

Reputation: 5472

In case you want to omit fields, but provide default values for that field, you should be using autovalue as documented at https://github.com/aldeed/meteor-simple-schema#autovalue

Alternatively, you can define your autoform attributes in your schema definition and keep them together as detailed here:

https://github.com/aldeed/meteor-autoform#putting-field-attribute-defaults-in-the-schema

If this is an update form and you are not looking for an autovalue, but rather keep the original value, then you can alternatively make that specific field a hidden input field as described at https://github.com/aldeed/meteor-collection2#problems so that it can still pass validation.

But I'd go with solid autovalue definitions.

Upvotes: 1

Related Questions