Implementing logical nested select in quickForm SimpleSchema - meteor

Property.attachSchema(new SimpleSchema({
'LGA': {
label: "L.G.A",
type: String,
allowedValues:
["Aba","Oha", "Enu"],
autoform: {
afFieldInput: {
firstOption: "(Pls, Select the L.G.A)"
}
}
}
,
'Town': {
label: "Town",
type: String,
allowedValues:
["Abakpa","Ewula", "Ezeoka", "Ubu","Echa", "Onu" ,"Eke", "Afor"],
autoform: {
afFieldInput: {
firstOption: "(Pls, Select the Ward)"
}
}
}
});
I have the schema above and I am using quickForm to implement this schema. "Abakpa","Ewula", "Ezeoka" are towns in "Aba" LGA while "Ubu","Echa", "Onu" are towns in "Oha" LGA and "Eke", "Afor" belong to "Enu" LGA. This is just for illustration because the list is quite numerous. I want a situation where if "Aba" is selected, the option that will be available for the Town select field would be "Abakpa","Ewula", "Ezeoka". If Oha is selected then "Ubu","Echa", "Onu" will be the next option in the Town schema etc. Pls, how do I implement this?.Thanks.

Related

Subschema as array item with AutoForm, SimpleSchema

I am trying to get subschemas to work as an array, which I assume is the correct way to handle my problem (but please correct me if I am wrong!). I provide a simplified working example to show my problem, based on the BooksSchema example provided by the AutoForm package. In my example, I have a collection of Libraries, and one of the fields in the 'Libraries' object is supposed to be the library's collection of books. Rendering the AutoForm does not give me any input labels as defined in my Book collection, but instead just shows one (1) empty text input field.
Schemas:
import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);
BooksSchema = new SimpleSchema({
title: {
type: String,
label: "Title",
max: 200
},
author: {
type: String,
label: "Author"
},
copies: {
type: Number,
label: "Number of copies",
min: 0
},
lastCheckedOut: {
type: Date,
label: "Last date this book was checked out",
optional: true
},
summary: {
type: String,
label: "Brief summary",
optional: true,
max: 1000
}
}, { tracker: Tracker });
LibrariesSchema = new SimpleSchema({
collection: {
type: Array
},
'collection.$': {
type: BooksSchema,
minCount: 1
}
});
LibrariesSchema.extend(BooksSchema);
Libraries = new Mongo.Collection("libraries");
Libraries.attachSchema(LibrariesSchema);
AutoForm:
{{> quickForm collection="Libraries" id="insertBookForm" type="insert"}}
Thank you so much in advance for your time, really been struggling with this for a long time now!
In my case I was indeed able to resolve the issue by using John Smith's example without the brackets.
LibrariesSchema = new SimpleSchema({
'books': {
type: BooksSchema,
minCount: 1
}
});
LibrariesSchema = new SimpleSchema({
'books': {
type: [BooksSchema],
minCount: 1
}
});
Arrays of a specific types, for use in check() or schema definitions, are specified as [SomeType], eg. [String], or [BooksSchema] in your case.

load collections to a quickform in meteor

I created two seperate schemas for payments collection and memberProfile. Now I need to create a quickform so I could load all the payments relevant to a unique memberProfile.
//The code for memberPayment collection
MemberProfiles = new Mongo.Collection('memberProfiles');
RecipeSchema = new SimpleSchema({
name: {
type: String,
label: "Name"
},
desc: {
type: String,
label: "Description"
},
payments:{
type: [PaymentSchema],
autoValue: function () {
return Payments.find({ memberId="uniqueId"});
},
defaultValue: function () {
return Payments.find({memberId="uniqueId"});
},
},
// The code for payments collection
PaymentSchema = new SimpleSchema({
name:{
type: String
},
amount:{
type: String
},
memberId:{
type: String
},
});
This code doesn't work.
Looks like you're missing the schema attribute. Any autoform needs to take in a schema attribute that explicitly tells autoform to use that schema to generate the necessary form. Check this page out for demos using autoform.
{{> quickForm collection="theMongoCollection" id="theFormID" schema="theSchemaName" type="typeOfForm" }}

Meteor + Autoform: how to show original when doing update with select-multiple dropdown?

I'm trying to build an update form for a document that has a select-multiple field in the Schema
I use quickForm
I have this on the schema side for the drop down:
channels: {
type: [String],
label: "Channels",
optional: false,
autoform: {
type: "select-multiple",
options: function () {
var dc = Channels.find({}).fetch();
return dc.map(function (channel) { return {label: channel.name, value: channel._id}});
}
}
}
on the form side, I pass the doc
{{> quickForm
collection="Blah"
id="updateChannels"
type="method-update"
meteormethod="updateBlah"
doc=this
}}
I get the fields from the document, but the select-multiple shows the list of options, without selected items.
How do I get the selected items to show selected in that list?

how to add extra attributes to the quickform before storing it in the database?

I have global variable that store the url of the uploaded image by the user.
how do i add that variable as an attribute in the document before adding it to the database?
here is my code
Meteor.methods({
submitPost: function (app) {
// Console.log('new App:', app);
check(app, {
title: String,
description: String,
category: String,
price: Number
});
Products.insert(app);
}
});
i want to add the global variable inside "app" before inserting it in Products collection
How do i do it?
This is what i added in the collection
previewImage: {
type: String,
autoValue: function(){
return PIurl;
},
autoform: {
type: "hidden"
}
},
createdAt:{
type: String,
autoValue: function(){
return new Date();
},
autoform: {
type: "hidden"
}
}
}));
after i added the above code, nothing happens when i click on submit, the form is no longer stored in the database
Two ways you can achieve this, the first is to use AutoForm.hooks onSubmit hook autoform hooks. The other way is to add it to your schema with the object attribute of autoValue :
Schema.something = new SimpleSchema({
category: {
type: String,
autoValue: function () {
return "foo";
}
},

Autoform: can I specify the options helper in the schema?

Is there any way to specify the options helper in the schema? I tried:
Schema
{
favoriteColor: {
type: String,
autoform: {
options: "colorOptions"
}
}
}
But it does not seem to work.
The following technique works fine to display a select with options in a form:
Schema
{
favoriteColor: {
type: String
}
}
Helper
Template.myFormTemplate.helpers({
colorOptions: function () {
return Colors.find().map(function (c) {
return {label: c.name, value: c._id};
});
}
});
Template
{{> afQuickField name="favoriteColor" options=colorOptions}}
In my actual schema I have an array of objects, and in each object I need to select an item from different collection. When you use afArrayField you can no longer set the options in the template as I did in the Template above (because it's an array of objects, and one element in the object would refer the helper).
Is my only option to query the database when I define the scheme? That I guess would make it non reactive, right?
{
favoriteColor: {
type: String,
autoform: {
options: function () {
return Colors.find().map(function (c) {
return {label: c.name, value: c._id};
});
}
}
}
}
Inserting the helper function directly into the schema will work. I'm doing something similar and it is reactive.

Resources