Autoform 6.2.0中的SimpleSchema自定义错误消息无法正常工作

时间:2017-10-12 12:30:34

标签: meteor simpl-schema

我关注Start Date to be Greater than End Date链接。以下是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.

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

  

注意:我正在使用Meteor 1.5.0与aldeed:autoform@6.2.0"simpl-schema": "^0.3.2"

1 个答案:

答案 0 :(得分: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 }作为options参数传递给new SimpleSchema()构造函数,以确保错误消息的反应性。

来源:Change Log: 2.0: Other Breaking Changes

  

客户端代码中标签和错误消息的反应性不再是自动的。在创建SimpleSchema个实例时,请在选项中传递{ tracker: Tracker }以启用跟踪器反应。