Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

```

like image 515
Alexey Ruzin Avatar asked Feb 13 '23 18:02

Alexey Ruzin


1 Answers

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

like image 141
Alexey Ruzin Avatar answered Feb 18 '23 22:02

Alexey Ruzin