I followed Start Date to be Greater than End Date link. Below is the SimpleSchema
code.
import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
import MessageBox from 'message-box';
SimpleSchema.extendOptions(['autoform']);
MessageBox.defaults({
en: {
startDateMustBeSmaller: "From Date must be greater than to Date"
}
});
export const Appointments = new Mongo.Collection('Appointments');
Appointments.allow({
insert: function(userId, doc){ return !!userId; },
update: function(userId, doc){ return !!userId; },
remove: function(userId, doc){ return !!userId; }
});
AppointmentsSchema = new SimpleSchema({
"fromDate": {
type: Date,
label: "From Date",
autoform: {
afFieldInput: {
type: "text",
}
}
},
"toDate": {
type: Date,
label: "To Date",
autoform: {
afFieldInput: {
type: "text",
}
},
custom: function() {
var start = this.field('fromDate');
var end = this;
if (start.isSet && end.isSet) {
if (moment(end.value).isBefore(start.value)) return "startDateMustBeSmaller";
}
}
}
});
Appointments.attachSchema( AppointmentsSchema );
Template.html
{{#autoForm id='insertAppointmentForm' collection=appointment type="insert" doc=this validation="browser"}}
<fieldset>
<div class="col-sm-6">
{{> afQuickField name='clientId' options=clientsSelect2 select2Options=s2Opts}}
</div>
<div class="col-sm-6">
{{> afQuickField name='otherDetails'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='fromDate'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='toDate'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='reason'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='meetingType'}}
</div>
</fieldset>
<div>
<button type="submit" class="btn btn-sm bg-olive margin">
<span class="glyphicon glyphicon-ok"></span> Create
</button>
<button type="submit" class="btn btn-sm bg-navy margin reset">
<span class="glyphicon glyphicon-refresh"></span> Reset
</button>
<a href="/user/view-appointments" class="btn btn-sm bg-orange margin pull-right" role="button">
<span class="glyphicon glyphicon-eye-open"></span>
View Appointments
</a>
</div>
{{/autoForm}}
When I try to run with above schema, the form do not get submitted, neither the client or server has error.
I also tried SimpleSchema.messages({})
, SimpleSchema.messageBox.messages({})
but I get method not found error.
Problem: I want to check if start date before end date. Above code does not work.
Note: I am using Meteor 1.5.0 with
aldeed:autoform@6.2.0
,"simpl-schema": "^0.3.2"