dmkanerer
dmkanerer

Reputation: 141

How can I make a field in a meteor Simple Schema equal to a js variable and still let the user fill out the rest of the simple schema?

I am making a meteor web app where the user will click on a html button. Once this button is clicked, the user needs to be directed to another page with some forms generated by a meteor simple schema package. The first field in the simple schema needs to automatically be given a string value of "hello" and then the rest of the fields in the simple schema will be filled out by the user with the input fields on the page. What I am unsure about is how to get the first value automatically set to this string value. Here is some of the code I have:

The simple schema declaration:

LobbySchema = new SimpleSchema({
  game: {
    type: String,
    label: "Game"
  },
  console: {
    type: String,
    label: "Console"
  },
  players: {
    type: Number,
    label: "Players"
  },
  mic: {
    type: Boolean,
    label: "Mic"
  },
  note: {
    type: String,
    label: "Note"
  },
  gamertag: {
    type: String,
    label: "Gamertag"
  },
  createdAt: {
    type: Date,
    label: "Created At",
    autoValue: function(){
      return new Date()
    },
    autoform: {
      type: "hidden"
    }
  }
}); 

The first field there in the schema "game" needs to be given the value "hello" when the html button is clicked. Right now I can assign that value to a javascript variable using the button by having an onclick function:

function getElementText(elementID){
   var elementText = "hello";
}

The button would call the getElementText function and have the elementText variable equal "hello". Now I need to assign the the first field in the simple schema to this variable value, "hello", then have it so the user can now fill out the rest of the schema with the input fields, automatically generated into the html with this code:

{{> quickForm collection="Lobby" id="insertLobbyForm" type="insert" class="newLobbyForm"}}

If you do not feel like providing the answer (maybe it happens to be more complicated than I think) then I would be very happy to receive a link to a site that might help me with this. I am also very willing to explain anything about the question if I did not explain the situation well enough above.

Upvotes: 2

Views: 212

Answers (1)

MrE
MrE

Reputation: 20828

You can use the AutoForm hooks like this:

AutoForm.hooks({
    app_create: {
        before: {
            method: function (doc) {
                // Do whatever assignment you need to do here, 
                // like doc['game'] = "hello";  //then 
                return doc;
            }
        },
        onSuccess: function (formType, result) {

        },
        onError: function (formType, error) {

        }
    }
});

Where here app_create is the id of the form you're sending with Autoform.

Upvotes: 1

Related Questions