I am using the very nice Autoform package in Meteor (https://github.com/aldeed/meteor-autoform). I have the reactive forms working great- but want to populate form data to allow editing based on the user selecting a row in a table. Very simple example could be found here:
http://autoform.meteor.com/insertaf
Effectively, I'd like to fill the form data in with data from the "Person" row that the user clicks for editing, but not sure how to do this. Any examples on how to do this would be much appreciated. Thanks!
Form code:
{{#autoForm id="afInsertDemo" type="insert" collection=Collections.People}}
<div class="form-group {{#if afFieldIsInvalid name='firstName'}}has-error{{/if}}">
<label class="control-label">{{afFieldLabelText name='firstName'}}</label>
{{> afFieldInput name='firstName'}}
{{#if afFieldIsInvalid name='firstName'}}
<span class="help-block">{{{afFieldMessage name='firstName'}}}</span>
{{/if}}
</div>
<div class="form-group {{#if afFieldIsInvalid name='lastName'}}has-error{{/if}}">
<label class="control-label">{{afFieldLabelText name='lastName'}}</label>
{{> afFieldInput name='lastName'}}
{{#if afFieldIsInvalid name='lastName'}}
<span class="help-block">{{{afFieldMessage name='lastName'}}}</span>
{{/if}}
</div>
<div class="form-group {{#if afFieldIsInvalid name='age'}}has-error{{/if}}">
<label class="control-label">{{afFieldLabelText name='age'}}</label>
{{> afFieldInput name='age'}}
{{#if afFieldIsInvalid name='age'}}
<span class="help-block">{{{afFieldMessage name='age'}}}</span>
{{/if}}
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Add Person</button>
<button type="reset" class="btn btn-default">Reset Form</button>
</div>
{{/autoForm}}
Meteor Javascript
Schemas = {};
UI.registerHelper("Schemas", Schemas);
Schemas.Person = new SimpleSchema({
firstName: {
type: String,
index: 1,
unique: true
},
lastName: {
type: String,
optional: true
},
age: {
type: Number,
optional: true
}
});
var Collections = {};
UI.registerHelper("Collections", Collections);
People = Collections.People = new Mongo.Collection("People");
People.attachSchema(Schemas.Person);
Meteor.publish(null, function () {
return People.find();
});
People.allow({
insert: function () {
return true;
},
remove: function () {
return true;
}
});
Just change
to
therefore you'll change your
type
property from insert to update and also add adoc
property to tell the autoform which doc it will update. You can return thedoc
from your template helper.The autoform documentation for the autoform component at https://github.com/aldeed/meteor-autoform#autoform-1 explains the
type
anddoc
attributes as:Note: I've also changed the
id
property so that you can reference to this form separately later on. But there are ways you can reuse a single form for update/insert as explained at https://github.com/aldeed/meteor-autoform#can-i-reuse-the-same-quickform-or-autoform-for-both-inserts-and-updates