Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 验证Meteor AutoForm SimpleSchema中的日期值_Javascript_Meteor_Meteor Autoform_Simple Schema - Fatal编程技术网

Javascript 验证Meteor AutoForm SimpleSchema中的日期值

Javascript 验证Meteor AutoForm SimpleSchema中的日期值,javascript,meteor,meteor-autoform,simple-schema,Javascript,Meteor,Meteor Autoform,Simple Schema,我有以下模式: Dates.attachSchema(new SimpleSchema({ description: { type: String, label: "Description", max: 50 }, start: { type: Date, autoform: { afFieldInput: { type: "bootst

我有以下模式:

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"
            }
        }
    }
}));
如何验证
结束
日期不在
开始
之前?我正在使用MomentJS来处理日期类型,但是我的主要问题是如何访问
自定义
函数中的其他属性

例如:

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


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

对于字段间通信,您可以执行以下操作:

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'
})
关于唯一性,首先简单模式本身不提供唯一性检查。您应该为此添加
aldeed:collection2

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

Dates._ensureIndex( { start: 1, end: 1 }, { unique: true } )
即使在此之后,您也无法在表单上看到此复合索引中的错误,因为autoform需要知道存在此类错误

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');
        }

      }
    }
  }
});