【问题标题】:Simple Schema minDate maxDate简单模式 minDate maxDate
【发布时间】:2016-02-05 12:18:53
【问题描述】:

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

路径:Schema.js

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

【问题讨论】:

    标签: meteor simple-schema


    【解决方案1】:

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

    【讨论】:

    • 谢谢@Stephen。自定义函数中如何引用endDate?
    • 对不起。我的意思是如何引用用户添加的 endDate。如 if (startDate > endDate) { return 'minDate'; //根据文档的错误字符串。 } 其他 { 返回真; }
    • 哦,我明白你在说什么。实际上,我只需要为我的项目做这件事——我在我的活动中做了客户端。我只是拿了日期选择器的值并比较了它们。如果 startDate > endDate,我抛出了一个错误并且没有执行任何服务器端逻辑。
    • 如果他们没有运行客户端验证,例如他们使用的是旧浏览器,是否会出现错误?
    • 不,在这种情况下,您没有使用 jQuery 验证,而是使用代码来验证并通知用户出错。
    猜你喜欢
    • 1970-01-01
    • 2015-02-11
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 2011-03-01
    • 2017-06-12
    • 1970-01-01
    相关资源
    最近更新 更多