Autoform - Setting an update form based on Session variable


#1

I’m trying to use Autoform to provide an update form for a document, where the document in question is set based on a Meteor Session variable.

The Autoform docs provide an example using iron-router’s data and this.

I have a template helper like so:

Template.ratings.helpers({
    currentSport: function () {
        var currentSport = Session.get('currentSport')
        return Rating.findOne(currentSport);
    }
});

Then my update form template:

<template name="updateRating">
    {{> quickForm collection="Ratings" doc=currentSport id="updateRatingForm" type="update"}}
</template>

For some reason, this just seems to produce a generic Add Autoform with a Submit button.

When I change the session variable (via a dropdown), the Autoform doesn’t update, nor doe the Submit do anything.

Any ideas on what’s going on?


#2

Ok, this is super weird - I actually managed to get the forms working - however, the AutoForm is being weird - it seems to have some issues with the Select2 and AllowedValues options, and also doesn’t auto-save.

These are my template helpers:

Template.updateRating.helpers({
    currentSport: function () {
        return Ratings.findOne(Session.get('currentSport'));
    }
});

These are my HTML templates:

<template name="updateRating">
    {{#if currentSport}}
        <p>Current sport is set</p>

        {{> quickForm collection="Ratings" doc=currentSport id="updateRatingForm" type="update" autosave=true}}

        {{#autoForm id="afUpdateDemo" type=update collection="Ratings" doc=currentSport autosave=true}}
            {{> afQuickField name="name"}}
            {{> afQuickField name="fooDate"}}
            {{> afQuickField name="Budget"}}
            {{> afQuickField name="staff"}}
        {{/autoForm}}

    {{else}}
        Please select a sport
    {{/if}}
</template>

And the schema:

Ratings.attachSchema(new SimpleSchema({
    name: {
        type: String,
        label: "Name of Sport",
        allowedValues: Sports,
        max: 200,
        autoform: {
            type: "select2"
        }
    },
    fooDate: {
        type: Date,
        label: "Date (What date?)"
    },
    Budget: {
        type: Number,
        label: "Budget"
    },
     staff: {
        type: String,
        label: "Coaching Staff",
        allowedValues: RAGG,
        max: 20,
        autoform: {
            type: "select2"
        }
    },
etc.

With the above, any String fields render as blank with Autoform, and if you try to open them to set it, it says “No results found” - however, the quickForm renders fine:

If however, I remove the Select2 option from the schema, the AutoForm (right-side) suddenly renders fine:

The quickForm (left-side) still seems to know to pick HTML select fields, the AutoForm just defaults to free-form string fields.

Not sure what’s going on.

And the autosave works for quickForm, but not for autoForm - if I change fields in quickForm, it appears to be sent correctly to the database, but with the autoForm, editing fields, you get nothing…

I’m assuming there’s something I’ve overlooked in the code at the top? Or is this some limitation of autoForms?