javascript - 在 Meteor AutoForm SimpleSchema 中验证日期值

标签 javascript meteor meteor-autoform simple-schema

我有以下架构:

Dates.attachSchema(new SimpleSchema({
    description: {
        type: String,
        label: "Description",
        max: 50
    },
    start: {
        type: Date,
        autoform: {
            afFieldInput: {
                type: "bootstrap-datepicker"
            }
        }
    },
    end: {
        type: Date,
        autoform: {
            afFieldInput: {
                type: "bootstrap-datepicker"
            }
        }
    }
}));

如何验证 end 日期不在 start 之前?我正在使用 MomentJS 来处理日期类型,但是我的主要问题是如何访问 custom 函数中的其他属性。

例如:

end: {
   type: Date,
   autoform: {
       afFieldInput: {
           type: "bootstrap-datepicker"
       }
   },
   custom: function() {
       if (moment(this.value).isBefore(start)) return "badDate";
   }
}

如何访问开始

此外,我如何验证 start + end 日期组合是否唯一,这意味着我的数据库中没有保存任何文档具有完全相同的 startend 日期?

最佳答案

对于域间通信,你可以这样做:

end: {
  type: Date,
  autoform: {
    afFieldInput: {
      type: "bootstrap-datepicker"
    }
  },
  custom: function() {
    // get a reference to the fields
    var start = this.field('start');
    var end = this;
    // Make sure the fields are set so that .value is not undefined
    if (start.isSet && end.isSet) {
      if (moment(end.value).isBefore(start.value)) return "badDate";
    }
  }
}

你当然应该先声明badDate错误

SimpleSchema.messages({
  badDate: 'End date must be after the start date.',
  notDateCombinationUnique: 'The start/end date combination must be unique'
})

关于唯一性,首先simple schema本身不提供唯一性校验。您应该为此添加 aldeed:collection2

此外,collection2 只能检查单个字段的唯一性。要完成复合索引,您应该使用 ensureIndex 语法

Dates._ensureIndex( { start: 1, end: 1 }, { unique: true } )

即使在这之后,您也无法从表单上的复合索引中看到错误,因为自动表单需要知道此类错误的存在。

AutoForm.hooks({
  NewDatesForm: { // Use whatever name you have given your form
    before: {
      method: function(doc) {
        var form = this;
        // clear the error that gets added on the previous error so the form can proceed the second time
        form.removeStickyValidationError('start');
        return doc;
      }
    },
    onSuccess: function(operation, result, template) {
      if (result) {
        // do whatever you want if the form submission is successful;
      }
    },
    onError: function(operation, error) {
      var form = this;
      if (error) {

        if (error.reason && error.reason.indexOf('duplicate key error')) {
          // We add this error to the first field so it shows up there
          form.addStickyValidationError('start', 'notDateCombinationUnique'); // of course you have added this message to your definition earlier on
          AutoForm.validateField(form.formId, 'start');
        }

      }
    }
  }
});

关于javascript - 在 Meteor AutoForm SimpleSchema 中验证日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33065326/

相关文章:

使用 0.6.5 部署 meteor

meteor - 如何对 [Object] 数组的每个单独元素使用 {{#autoForm}}?

javascript - 使用 AngularJS 验证单选按钮集

javascript - Handsontable Select2 动态选项

javascript - 在 JavaScript 中使用月/日/小时/分钟/秒

javascript - 根据另一个 Meteor 集合的属性过滤一个 Meteor 集合

node.js - 如何结合使用node-imap和Meteor?

meteor - 自动生成 : can I specify the options helper in the schema?

javascript - 如何从自动表单 Hook 访问模板实例数据

javascript - 使用动画展开/折叠菜单列表