bp123
bp123

Reputation: 3427

Simple Schema minDate maxDate

I think this is a simple question. I'm using simple schema and I want to have a minDate and maxDate. The documentation talks about it in validation section but I'm not sure how to define it in the schema itself. Any help would be great. Thanks

Path: Schema.js

startDate: {
        type: Date,
        optional: true,
        autoform: {
            type: "bootstrap-datepicker"
          }
    },
    endDate: {
        type: Date,
        optional: true,
        autoform: {
            type: "bootstrap-datepicker"
          }
    }

Upvotes: 2

Views: 503

Answers (1)

Stephen Woods
Stephen Woods

Reputation: 4049

I found an issue in the simple-schema repo that goes over this. Here's how your code might look with a static min/max date:

startDate: {
    type: Date,
    optional: true,
    min: new Date(2016, 1, 1),
    autoform: {
        type: "bootstrap-datepicker"
    }
},
endDate: {
    type: Date,
    optional: true,
    max: new Date(2018, 1, 1),
    autoform: {
        type: "bootstrap-datepicker"
    }
}

You can use the custom validator if you want to make those dates dynamic. Here's a link to the relevant documentation. Your start date would look something like this:

startDate: {
    type: Date,
    optional: true,
    custom: function() {
        var myMinDate = new Date(); //today
        if(myMinDate > this.value) {
            return 'minDate';  //Error string according to the docs.
        } else {
            return true;
        }
    },
    autoform: {
        type: "bootstrap-datepicker"
    }
},
endDate: {
    type: Date,
    optional: true,
    custom: function() {
        var myMaxDate = new Date(2018, 11, 31); //Last day of 2018
        if(myMaxDate < this.value) {
            return 'maxDate';  //Error string according to the docs.
        } else {
            return true;
        }
    },
    autoform: {
        type: "bootstrap-datepicker"
    }
}

Upvotes: 3

Related Questions