Autoform 6.2.0 中的 SimpleSchema 自定义错误消息不起作用
SimpleSchema custom error messages in autoform 6.2.0 not working
我关注了link。下面是 SimpleSchema
代码。
import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
import MessageBox from 'message-box';
SimpleSchema.extendOptions(['autoform']);
MessageBox.defaults({
en: {
startDateMustBeSmaller: "From Date must be greater than to Date"
}
});
export const Appointments = new Mongo.Collection('Appointments');
Appointments.allow({
insert: function(userId, doc){ return !!userId; },
update: function(userId, doc){ return !!userId; },
remove: function(userId, doc){ return !!userId; }
});
AppointmentsSchema = new SimpleSchema({
"fromDate": {
type: Date,
label: "From Date",
autoform: {
afFieldInput: {
type: "text",
}
}
},
"toDate": {
type: Date,
label: "To Date",
autoform: {
afFieldInput: {
type: "text",
}
},
custom: function() {
var start = this.field('fromDate');
var end = this;
if (start.isSet && end.isSet) {
if (moment(end.value).isBefore(start.value)) return "startDateMustBeSmaller";
}
}
}
});
Appointments.attachSchema( AppointmentsSchema );
Template.html
{{#autoForm id='insertAppointmentForm' collection=appointment type="insert" doc=this validation="browser"}}
<fieldset>
<div class="col-sm-6">
{{> afQuickField name='clientId' options=clientsSelect2 select2Options=s2Opts}}
</div>
<div class="col-sm-6">
{{> afQuickField name='otherDetails'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='fromDate'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='toDate'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='reason'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='meetingType'}}
</div>
</fieldset>
<div>
<button type="submit" class="btn btn-sm bg-olive margin">
<span class="glyphicon glyphicon-ok"></span> Create
</button>
<button type="submit" class="btn btn-sm bg-navy margin reset">
<span class="glyphicon glyphicon-refresh"></span> Reset
</button>
<a href="/user/view-appointments" class="btn btn-sm bg-orange margin pull-right" role="button">
<span class="glyphicon glyphicon-eye-open"></span>
View Appointments
</a>
</div>
{{/autoForm}}
当我尝试 运行 使用上述模式时,表单没有提交,客户端或服务器都没有错误。
我也尝试了 SimpleSchema.messages({})
、SimpleSchema.messageBox.messages({})
但我得到了 method not found error.
问题: 我想检查开始日期是否早于结束日期。以上代码无效。
Note: I am using Meteor 1.5.0 with aldeed:autoform@6.2.0
, "simpl-schema": "^0.3.2"
既然你没有 post 你的 Moment.js
版本,我假设你根本不使用它,而提到的答案确实如此。
您的问题在这一行:
if (moment(end.value).isBefore(start.value)) return "startDateMustBeSmaller";
您的两个字段值的类型都是 Date
,因此您可以直接比较它们:
if (end.value <= start.value) {
return 'startDateMustBeSmaller';
}
接下来,消息的问题:SimpleSchema.messages
和 SimpleSchema.prototype.messages
都已删除,如 Change Log: 2.0: Other Breaking Changes: Error message changes 中所述,但文档似乎尚未更新。
要自定义您的错误消息,您应该这样做:
// add this right after AppointmentsSchema definition
AppointmentsSchema.messageBox.messages({
en: {
startDateMustBeSmaller: 'To Date must be greater than From Date',
}
});
已添加:
另一个关键点是将 { tracker: Tracker }
作为选项参数传递给 new SimpleSchema()
构造函数,以确保错误消息的反应性。
来源:Change Log: 2.0: Other Breaking Changes:
Reactivity of labels and error messages in client code is no longer automatic. When creating your SimpleSchema
instance, pass { tracker: Tracker }
in the options to enable Tracker reactivity.
我关注了SimpleSchema
代码。
import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
import MessageBox from 'message-box';
SimpleSchema.extendOptions(['autoform']);
MessageBox.defaults({
en: {
startDateMustBeSmaller: "From Date must be greater than to Date"
}
});
export const Appointments = new Mongo.Collection('Appointments');
Appointments.allow({
insert: function(userId, doc){ return !!userId; },
update: function(userId, doc){ return !!userId; },
remove: function(userId, doc){ return !!userId; }
});
AppointmentsSchema = new SimpleSchema({
"fromDate": {
type: Date,
label: "From Date",
autoform: {
afFieldInput: {
type: "text",
}
}
},
"toDate": {
type: Date,
label: "To Date",
autoform: {
afFieldInput: {
type: "text",
}
},
custom: function() {
var start = this.field('fromDate');
var end = this;
if (start.isSet && end.isSet) {
if (moment(end.value).isBefore(start.value)) return "startDateMustBeSmaller";
}
}
}
});
Appointments.attachSchema( AppointmentsSchema );
Template.html
{{#autoForm id='insertAppointmentForm' collection=appointment type="insert" doc=this validation="browser"}}
<fieldset>
<div class="col-sm-6">
{{> afQuickField name='clientId' options=clientsSelect2 select2Options=s2Opts}}
</div>
<div class="col-sm-6">
{{> afQuickField name='otherDetails'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='fromDate'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='toDate'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='reason'}}
</div>
<div class="col-sm-6">
{{> afQuickField name='meetingType'}}
</div>
</fieldset>
<div>
<button type="submit" class="btn btn-sm bg-olive margin">
<span class="glyphicon glyphicon-ok"></span> Create
</button>
<button type="submit" class="btn btn-sm bg-navy margin reset">
<span class="glyphicon glyphicon-refresh"></span> Reset
</button>
<a href="/user/view-appointments" class="btn btn-sm bg-orange margin pull-right" role="button">
<span class="glyphicon glyphicon-eye-open"></span>
View Appointments
</a>
</div>
{{/autoForm}}
当我尝试 运行 使用上述模式时,表单没有提交,客户端或服务器都没有错误。
我也尝试了 SimpleSchema.messages({})
、SimpleSchema.messageBox.messages({})
但我得到了 method not found error.
问题: 我想检查开始日期是否早于结束日期。以上代码无效。
Note: I am using Meteor 1.5.0 with
aldeed:autoform@6.2.0
,"simpl-schema": "^0.3.2"
既然你没有 post 你的 Moment.js
版本,我假设你根本不使用它,而提到的答案确实如此。
您的问题在这一行:
if (moment(end.value).isBefore(start.value)) return "startDateMustBeSmaller";
您的两个字段值的类型都是 Date
,因此您可以直接比较它们:
if (end.value <= start.value) {
return 'startDateMustBeSmaller';
}
接下来,消息的问题:SimpleSchema.messages
和 SimpleSchema.prototype.messages
都已删除,如 Change Log: 2.0: Other Breaking Changes: Error message changes 中所述,但文档似乎尚未更新。
要自定义您的错误消息,您应该这样做:
// add this right after AppointmentsSchema definition
AppointmentsSchema.messageBox.messages({
en: {
startDateMustBeSmaller: 'To Date must be greater than From Date',
}
});
已添加:
另一个关键点是将 { tracker: Tracker }
作为选项参数传递给 new SimpleSchema()
构造函数,以确保错误消息的反应性。
来源:Change Log: 2.0: Other Breaking Changes:
Reactivity of labels and error messages in client code is no longer automatic. When creating your
SimpleSchema
instance, pass{ tracker: Tracker }
in the options to enable Tracker reactivity.