2017-10-12 73 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

我的意思是沒有行動發生,表單是正常的,即我仍然可以把正確的條目,它的工作原理。但是當我把錯誤的日期,沒有行動發生。 –