meteormeteor-autoformsimple-schemameteor-collection2

How to pass a default value for a field in 'insert' form?


How to pass a default value for a field in 'insert' form?

I'm using Meteor's packages: Autoform, Collections2, and Simple-Schema.

My process is:

  1. A User selects some value in a list on a page, then
  2. The 'insert' from opens, and I want that one field to be initialized with the value the user chose on a previous step.

Can't figure out how to pass a parameter withing URL (or any other way). The problem is how to initialize form with the value.

Suppose I have an URL:

http://localhost:3000/activity/new/Sport

=============== router.js:

...
Router.map(function () {
    ...
    this.route('newActivity', {
        path: '/activity/new/:tag',
        data: function() {
            Session.set('tag', this.params.tag);
            return null;
        }
    });
    ...

=============== models/activity.js

...
Activities = new Meteor.Collection("activities", {
    schema: {
        title: {
            type: String,
            label: 'название'
        },
        ...
        tag: {
            type: String,
            label: 'тэг'
        }
    }
});

================ templates/avtibity.js

...
Template.newActivity.helpers({
    defaultTag: function() {
        return Session.get('tag');
    }
});
...

================ templates/activity.html

...
<template name="newActivity">
    <h1>Create new Activity!</h1>
    {{#autoForm collection="Activities" id="insertActivityForm" type="insert"}}
        {{> afQuickField name="title"}}
        ...
        {{> afQuickField name="tag" value="   ??????    "}} // ? {{defaultTag}}
        ho ho ho {{defaultTag}}
    {{/autoForm}}
</template>

```


Solution

  • Thanks to Eric Dobbertin:

    1. You could set value equal to a helper that returns the desired value ({{> afQuickField name="tag" value=valueHelper}})
    2. List item You could set doc to an object that has the value set to what you want. Just like you would for an update form.

    https://github.com/aldeed/meteor-autoform/issues/210