How to pass _id to autoForm via hidden field in Meteor?

I have a collection of projects and the Meteor.users collection. My goal is to add the document _id of a project to a username.

HTML:

<template name="insertName">
    {{#each projectList}}
        {{> projectView}}
    {{/each}}
</template>

<template name="projectView">
    {{#autoForm collection="Meteor.users" id="insertUser" type="insert"}}
         {{> afQuickField name="username"}}
         // From here I can access the _id property from the projects collection
         // Question: How do I pass it to my form without creating an input field?
    {{/autoForm}}
</template>

JS:

Meteor.users = new Meteor.Collection('projects');
Meteor.users.attachSchema(new SimpleSchema({
    username: {
        type: String,
        max: 50
    },
    projectId: {
        type: String,
        max: 20
        // Use autoValue to retrieve _id from projects collection?
    }
});

Using the HTML code above, I know I could write this:

{{> afQuickField name='projectId' value=this._id}}

This, however, creates an input field, which I don't want. How can I pass the _id to my autoForm without having a visible input field? Or maybe someone can point me into a different direction?

Upvotes: 3

Views: 1400

Answers (2)

Serkan Durusoy
Serkan Durusoy

Reputation: 5472

There are multiple ways of attacking this problem, but basically, if you want a hidden field, you must set the type of the autoform field to hidden.

You could do either:

{{> afQuickField name='projectId' value=this._id type='hidden'}}

or do that on the schema itself

Meteor.users = new Meteor.Collection('projects');
Meteor.users.attachSchema(new SimpleSchema({
    username: {
        type: String,
        max: 50
    },
    projectId: {
        type: String,
        max: 20,
        autoform: {
          type: 'hidden'
        }
    }
});

Upvotes: 4

Winston RIley
Winston RIley

Reputation: 482

Im not confident this is the best solution, but I have seen this as a way of side stepping the issue you are having:

{{> afQuickField name='projectId' value=this._id type='hidden'}}

Upvotes: 1

Related Questions