在Meteor AutoForm SimpleSchema中验证日期值 [英] Validate date values in Meteor AutoForm SimpleSchema
问题描述
我有以下架构:
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
函数中的其他属性.
How can I validate that the end
date is not before start
? I am using MomentJS to handle date types, however my main problem is how I can access other attributes in the custom
function.
例如:
end: {
type: Date,
autoform: {
afFieldInput: {
type: "bootstrap-datepicker"
}
},
custom: function() {
if (moment(this.value).isBefore(start)) return "badDate";
}
}
如何访问start
?
此外,如何验证start
+ end
日期组合是否为唯一,这意味着数据库中没有保存与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?
推荐答案
对于域间通信,您可以执行以下操作:
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";
}
}
}
您当然应该先声明badDate
错误
SimpleSchema.messages({
badDate: 'End date must be after the start date.',
notDateCombinationUnique: 'The start/end date combination must be unique'
})
关于唯一性,首先,简单模式本身不提供唯一性检查.您应该为此添加aldeed:collection2
.
Regarding the uniqueness, first of all simple schema itself does not provide uniqueness check. You should add aldeed:collection2
for that.
此外,collection2只能检查单个字段的唯一性.要完成复合索引,您应该使用ensureIndex
语法
Furthermore, collection2 is capable of checking only a single field uniqueness. To accomplish compound indexes, you should use the ensureIndex
syntax
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.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');
}
}
}
}
});
这篇关于在Meteor AutoForm SimpleSchema中验证日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!