简单架构minDate maxDate

时间:2016-02-05 02:16:21

标签: meteor simple-schema

我认为这是一个简单的问题。我使用简单的架构,我希望有一个minDate和maxDate。文档在验证部分讨论了它,但我不确定如何在模式本身中定义它。任何帮助都会很棒。感谢

路径:Schema.js

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

1 个答案:

答案 0 :(得分:3)

我在simple-schema repo中发现了一个问题。以下是静态最小/最大日期代码的外观:

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"
    }
}

如果您想使这些日期动态化,可以使用custom验证程序。这是相关文档的a link。您的开始日期如下所示:

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"
    }
}
相关问题