Search code examples
meteormeteor-autoform

Creating meteor autoform w/ array of radio buttons


I'd like to build an autoform in meteor that presents user with a twelve radio buttons and records an entry for each of the 12 buttons. I can get the form working easily enough if I create 12 different buttons—see below, but I'm wondering if it is possible to create an array to do this a bit more easily.

Homework = new Mongo.Collection("homework");
Homework.attachSchema(new SimpleSchema({
userId:{
    type: String,
    autoValue:function(){return this.userId},

},
userName:{
    type: String,
    autoValue:function(){return Meteor.users.findOne({_id: this.userId}).emails[0].address},
},
fName:{
    type: String,
    autoValue:function(){return Meteor.users.findOne({_id: this.userId}).profile.fName},
},
lName:{
    type: String,
    autoValue:function(){return Meteor.users.findOne({_id: this.userId}).profile.lName},
},

page: {
    type: Number,
    label: "Page Number",
    max: 200
},
problem1: {
    type: Boolean,
},
problem2: {
    type: Boolean,
},
problem3: {
    type: Boolean,
},
problem4: {
    type: Boolean,
},
problem5: {
    type: Boolean,
},
problem6: {
    type: Boolean,
},
problem7: {
    type: Boolean,
},
problem8: {
    type: Boolean,
},
problem9: {
    type: Boolean,
},
problem10: {
    type: Boolean,
},
problem11: {
    type: Boolean,
},
problem12: {
    type: Boolean,
},

}));

Solution

  • Yup! You can simple define arrays in SimpleSchema using this notation:

    ...
    
      'problems': {
        type: String,
        autoform: {
          afFieldInput: {
            options: function () { return {
              one: 'one',
              two: 'two',
              three: 'three',
              four: 'four',
              five: 'five',
              six: 'six',
            } }
          }
        }
      }
    
    ...
    

    And in your template

    {{ > afQuickField name="problems" noselect=true }}