2017-10-12 58 views
1

我跟着Start Date to be Greater than End Date链接。以下是SimpleSchema的代码。SimpleSchema autoform 6.2.0中的自定义错误消息不起作用

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.

问题:我想检查是否结束日期之前开始日期。以上代码不起作用。

注:我使用流星1.5.0 aldeed:[email protected]"simpl-schema": "^0.3.2"

回答

0

看到你没有张贴您的Moment.js版本,我假设你不使用它在所有的,而提到的答案是。

你的问题是在这条线:

if (moment(end.value).isBefore(start.value)) return "startDateMustBeSmaller"; 

无论你的字段中的值有Date类型,所以你可以对它们进行比较:

if (end.value <= start.value) { 
    return 'startDateMustBeSmaller'; 
} 

接下来,邮件的问题: SimpleSchema.messagesSimpleSchema.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:在客户端代码的标签和错误消息

反应不再自动的。在创建您的SimpleSchema实例时,请在{ tracker: Tracker }的选项中启用跟踪器反应性。

+0

仍然无效。页面挂起。服务器或客户端没有错误。 –

+0

@AnkurSoni你是什么意思的“页面挂起”?用户界面无响应? – Styx

+0

我的意思是没有行动发生,表单是正常的,即我仍然可以把正确的条目,它的工作原理。但是当我把错误的日期,没有行动发生。 –