Autoform: variable is not in the window scope - meteor

I am trying to display an autoform on the main page of the website by attaching a schema to it.
I get the following error:
Uncaught Error: Inventory is not in the window scope
server.js:
SimpleSchema.extendOptions(['autoform']);
import SimpleSchema from 'simpl-schema';
Inventory = new Mongo.Collection('inventory');
Inventory.attachSchema(new SimpleSchema({
customTonerName: {
type: String,
label: 'Custom Toner'
},
quantity: {
type: Number,
label: 'Quantity'
}
}));
Main template:
{{#autoForm collection="Inventory" id="insertInventoryForm" type="insert"}}
{{> afQuickField name='quantity'}}
{{/autoForm}}

Providing you the 2 solutions for this in the order od recommendation.
Solution No. 1 : Just make a simple helper function in .js file as below,
Main.js
import { Inventory } from 'your location'; // mention path here
Template.Main.helpers({
Inventory(){
return Inventory;
}
});
Main.html
{{#autoForm collection=Inventory id="insertInventoryForm" type="insert"}}
{{> afQuickField name='quantity'}}
{{/autoForm}}
Solution No.2 : Import your collections on a Main.js client file and add them to the window scope.
Main.js
import { Inventory } from 'your location'; // mention path here
window.Inventory = Inventory;
Main.html
{{#autoForm collection="Inventory" id="insertInventoryForm" type="insert"}}
{{> afQuickField name='quantity'}}
{{/autoForm}}
Note: For more insight on this , click here

Related

Meteor Error: Missing required parameters on path The missing params are: ["_id"]. The params object passed in was: {}

I have this in my routes:
Router.map(function() {
...
this.route('studentEdit', {
path: '/student/:_id/edit',
data: function() {
return Students.findOne(this.params._id);
},
});
this.route('studentDetail', {
path: '/student/:_id',
data: function() {
return Students.findOne(this.params._id);
}
});
...
});
And I have this in my template using autoform:
{{#autoForm collection="Students" id="studentEdit" doc=this type="update"}}
{{> afQuickField name='name'}}
{{> afQuickField name='phone'}}
{{> afQuickField name='address' rows=6}}
{{> afQuickField name='remarks' rows=6}}
<button type="submit" class="btn waves-effect waves-light"><i class="material-icons">save</i></button>
{{/autoForm}}
The edit page loads fine, with the prepopulated fields. And when I save, it does save, yet, it doesn't redirect to the detail page, and returns this error in console:
Exception in delivering result of invoking '/students/update': Error: Missing required parameters on path "/student/:_id". The missing params are: ["_id"]. The params object passed in was: {}.
UPDATE
Routing to the detail page now works, yet the error still exist in the console. I must be missing something. This is what I've done to get it working for the time being:
var moveOnRouter = {
onSuccess: function(formType, result) {
Router.go('studentDetail', {_id: this.docId});
}
}
AutoForm.addHooks('studentEdit', moveOnRouter);
You need to explicitly go to the other route on submit from your form. But since your button is a submit you also need to prevent the default submit action.
With template events you'd do something like:
Template.myTemplate.events({
'submit .btn'(ev) {
ev.preventDefault();
router.go('studentDetail',{ _id: this.docId });
}
});
But since you're hooking autoform perhaps it's easier just to remove the type="submit" from your button definition.

Inserting a file into an array in Meteor using aldeed:autoform, cfs:autoform and the update-pushArray type

I'd like to build an array of uploaded files in each document in my collection named Modules. I'm using the following packages:
aldeed:autoform
aldeed:collection2
cfs:standard-packages
cfs:gridfs
cfs:autoform
Collection and Schema (relevant parts):
Modules = new Mongo.Collection('modules');
Modules.attachSchema (new SimpleSchema({
slides: {
type: Array,
optional: true
},
'slides.$': {
type: Object
},
'slides.$.fileId': {
type: String,
label: "Image File"
},
'slides.$.time': {
type: Number,
label: "Time in Seconds"
}
}));
FileStore = new FS.Collection("fileStore", {
stores: [new FS.Store.GridFS("fileStore")]
});
FileStore.allow({
download: function() {
return true;
},
fetch: null
});
In the HTML Template:
{{#autoForm collection="Modules" scope="slides" id="addSlideForm" type="update-pushArray" doc=this}}
<fieldset>
{{> afQuickField name="time" type="number"}}
{{> afQuickField name="fileId" type="cfs-file" collection="fileStore"}}
</fieldset>
<button type="submit" class="btn btn-primary" >Add Slide</button>
{{/autoForm}}
When I hit the submit button, an element is pushed into the array as expected. The time value is correct, but under fileId there is only dummyId instead of the expected _id from fileStore.
In other parts of the application that do not involve nested arrays, uploading files works as expected. In other parts of the application that do not involve uploading files, the update-pushArray form works as expected. The complication is with combining the two.
Am I doing this incorrectly? Or is cfs:autoform just not compatible with the update-pushArray form type?
To use CFS your #autoform type must be either "insert" or "method", check cfs documentation for more info.
Hope it helps!

Meteor autoform "afFieldValueIs" with a boolean checkbox only triggers once

I have a checkbox that needs to show/hide another input box. I'm doing the following:
Schema:
isFlexibleTime:
type: Boolean
label: 'Is the start time flexible?'
flexibleTimeDetails:
type: String
label: 'Flexible time details'
optional: true
Template:
+afQuickField(name='isFlexibleTime')
if afFieldValueIs name='isFlexibleTime' value=true
+afQuickField(name='flexibleTimeDetails')
The helper will trigger one time and show the other field but it won't trigger again. Any help into what is wrong would be much appreciated.
EDIT
Actually on further inspection it seems there is currently a bug with the checkbox event as of AutoForm 5.1.2 https://github.com/aldeed/meteor-autoform/issues/861
The issue has been open a little while, so you can use a quick workaround like:
In your template event:
'click [name=isFlexibleTime]': function() {
Session.set('isFlexibleTime', AutoForm.getFieldValue('isFlexibleTime','ID_OF_YOUR_AUTOFORM'));
}
Template helper:
isChecked: function() {
return Session.get('isFlexibleTime');
}
then:
{{#if isChecked}}
{{> afQuickField name="flexibleTimeDetails"}}
{{/if}}
I'm not sure if that's your actual syntax but following the example from: http://autoform.meteor.com/fieldvalues it should look like this:
{{> afQuickField name="isFlexibleTime"}}
{{#if afFieldValueIs name="isFlexibleTime" value="true"}}
{{> afQuickField name="flexibleTimeDetails"}}
{{/if}}

Meteor autoform access nested property

I've got some linked schemas and am trying to access properties of a subschema from the form for the primary schema. It's a mouthful, I know. Code may help:
//js
Collection = new Meteor.Collection('collection');
Schemas = {};
Schemas.secondary = new SimpleSchema({
unitType: {
type: String,
autoform: {
type: 'select',
options: //function with all the options
}
}
});
Schemas.primary= new SimpleSchema({
name: {
type: String,
},
units: {
type: [ Schemas.secondary ]
}
});
Collection.attachSchema(Schemas.primary);
//HTML
{{#autoForm collection="Collection" id="someId" type="insert"}}
{{> afQuickField name='name'}} // this works
{{> afQuickField name='units'}} // this works
{{> afQuickField name='units.unitType'}} // this doesn't work :-(
{{/autoForm}}
The reason I'm doing this is because there are other properties in the secondary schema that I want to show conditionally, based on the value of the select box. I also tried to put a form inside a form and then run {{#each afFieldNames name='"units"}} but that didn't quite work either. Instead of giving me just the fields contained in units (i.e., the secondary schema), it looped through all fields of both primary and secondary.
Thoughts? I'm not married to this pattern but I can't think of another way.
Thanks again, all.
db
I had this issue myself.
Give this a go
{{> afQuickField scope='units.unitType' name='units.unitType'}}
If you dump your modifier in your before submit hook you should be able to see the subdocument successfully filled out
AutoForm.hooks({
someId: {
before: {
'insert': function(modifier) {
console.log(modifier);
}
}
}
});
Let me know if this works for you!
All the best,
Elliott

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:
A User selects some value in a list on a page, then
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>
```
Thanks to Eric Dobbertin:
You could set value equal to a helper that returns the desired value ({{> afQuickField name="tag" value=valueHelper}})
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

Resources