Nighthawk
Nighthawk

Reputation: 142

Meteor get data from form that is defined by a schema

I am trying to set up a form that is for user creation. The emails portion of the schema is set up as an array:

...
"emails.$.address": {
    type: String,
    blackbox: true
},
"emails.$.verified": {
    type: Boolean,
    optional: true,
    blackbox: true
},
...

I use auto form to create the form inside of a template:

    {{#autoForm id="addUser" type="method" meteormethod="createUserwRole" collection="Users" schema=schema resetOnSuccess="true" }}
    <fieldset>
        {{> afQuickField name="fName" id="fName"}}
        {{> afQuickField name="lName" id="lName"}}
        {{> afQuickField name="username" id="username"}}
        {{> afQuickField name="emails" id="emails"}}
        {{> afFormGroup name="roles" options=options firstoption="Select Role" type="select-multiple" id="roles"}}
    <div>
      <button type="submit" class="btn btn-primary" data-toggle="modal" data-target="#addUser">Submit</button>
      <button type="reset" class="btn btn-default">Reset</button>
    </div>
  </fieldset>
  {{/autoForm}}

Then on submission I want to grab the data:

Template.addUser.events({
  'submit #addUser': function(e, t) {
    console.log("hit");
    console.log(t);
    e.preventDefault();
    var email = t.find("#emails.0.address").value;
    var username = t.find("#username").value;
    console.log(email);

        Meteor.call("createUserwRole", ({"email":email, "username":username}));
    }
});

But attempting to find emails.0.address returns the error:

TypeError: null is not an object (evaluating 't.find("#emails.0.address").value')

Upvotes: 1

Views: 238

Answers (1)

kaoskeya
kaoskeya

Reputation: 1091

There are too many things done incorrectly. Please go through the simple-schema and autoform README file. It is a pretty powerful package and does make a lot of things easier.

  1. Your Schema. Use blackbox: true only if the field type is an Object.

  2. Your Autoform. You have specified both collection="Users" schema=schema, please specify only one.

  3. Your form handling. Use AutoForm.hooks instead of custom event listeners.

    AutoForm.hooks({ addUser: { onSubmit: function (doc) { console.log( doc.emais[0].address ); console.log( doc.username ); return true; } } });

Upvotes: 2

Related Questions