Skip to content

Instantly share code, notes, and snippets.

@dohomi
Last active August 29, 2015 14:00
Show Gist options
  • Save dohomi/11198681 to your computer and use it in GitHub Desktop.
Save dohomi/11198681 to your computer and use it in GitHub Desktop.
SimpleSchema in Collection2 with autoValue
{{#autoForm collection=Collections.Clubs id="createClubForm" type="method" meteormethod="createClub"}}
<div class="form-group {{#if afFieldIsInvalid name='name'}}has-error{{/if}}">
<div class="input-group">
<span class="input-group-addon">
Name
</span>
{{>afFieldInput name="name"}}
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">
<span class="glyphicon glyphicon-save"></span> Save
</button>
</span>
</div>
<div class="help-block">
{{afFieldMessage name="name"}}
</div>
</div>
{{/autoForm}}
Collections.Clubs = new Meteor.Collection("clubs", {
schema: new SimpleSchema({
name: {
type: String,
label: "Name"
},
owner: {
type: String,
autoValue: function () {
if (this.isInsert) {
return Meteor.userId();
} else if (this.isUpsert) {
return {$setOnInsert: Meteor.userId()};
} else {
this.unset();
}
},
denyUpdate: true
// optional: true // not working if not set currently
},
// Force value to be current date (on server) upon insert
// and prevent updates thereafter.
createdAt: {
type: Date,
autoValue: function () {
if (this.isInsert) {
return new Date;
} else if (this.isUpsert) {
return {$setOnInsert: new Date};
} else {
this.unset();
}
},
denyUpdate: true
// optional: true // not working if not set currently
}
}
}
UI.registerHelper("Collections", Collections);
Meteor.methods({
/**
* Create club
*
* @param club
*/
"createClub": function (club) {
// clean autoForm schema while autoValue is in use
Collections.Clubs.simpleSchema().clean(club);
// check the form
check(club, Collections.Clubs.simpleSchema()); // ->ERROR shown Match failed [400]
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment