简体   繁体   English

在Meteor AutoForm SimpleSchema中验证日期值

[英]Validate date values in Meteor AutoForm SimpleSchema

I have the following 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"
            }
        }
    }
}));

How can I validate that the end date is not before start ? 如何在start之前验证end日期? I am using MomentJS to handle date types, however my main problem is how I can access other attributes in the custom function. 我使用MomentJS来处理日期类型,但我的主要问题是我如何访问custom函数中的其他属性。

For instance: 例如:

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

How can I access start ? 我怎样才能访问start

Furthermore, how can I validate if the start + end date combination is unique , meaning there is no document saved in my database which has the exact same start and end date? 此外,如何验证start日期和end日期组合是否唯一 ,这意味着我的数据库中没有保存的文档具有完全相同的start日期和end日期?

For the inter-field communication, you can do: 对于现场间通信,您可以:

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

You should of course declare the badDate error first 您当然应该首先声明badDate错误

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

Regarding the uniqueness, first of all simple schema itself does not provide uniqueness check. 关于唯一性,首先简单模式本身不提供唯一性检查。 You should add aldeed:collection2 for that. 你应该添加aldeed:collection2

Furthermore, collection2 is capable of checking only a single field uniqueness. 此外,collection2只能检查单个字段唯一性。 To accomplish compound indexes, you should use the ensureIndex syntax 要完成复合索引,应使用ensureIndex语法

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

Even after this, you will not be able to see the error from this compound index on your form because autoform needs to be aware that such error is existing. 即使在此之后,您也无法在表单上看到此复合索引的错误,因为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');
        }

      }
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM