0

如何将自定义验证消息发送到另一个模式字段?

SessionSchema = new SimpleSchema({
  'seat.from': {
    type: String,
    max: 10,
    optional: false
  },
  'seat.to': {
    type: String,
    max: 10,
    optional: false
  }
});

ReservationSchema = new SimpleSchema({
  title: {
    type: String
  },
  sessions: {
    type: [SessionSchema],
    min: 1,
    optional: false,
    custom: function() {
     //Its an array object. validation is depends on next array so I made a validation here instead of `SessionSchema`. 
     return "greater-session"; // dispaly error on top of the session. I need to display error message on perticular field in `SessionSchema`. 
    }
  }
});

SimpleSchema.messages({
    "greater-session": "From seat should not lesser then previous session"
});

自动成型:

{{#autoForm id="addReservation" type="method" meteormethod="insertMyReservation" collection="Reservation"}}
 {{> afQuickField name="title" autofocus=''}}
 {{> afQuickField name="sessions" panelClass="group"}}
{{/autoForm}}

我如何实现这一目标?

4

2 回答 2

0

我建议您为您的 SimpleSchema使用自定义验证器。他们可以访问更多上下文信息。

于 2015-11-16T13:45:55.610 回答
0

我会尝试这样的事情:

ReservationSchema = new SimpleSchema({
  title: {
    type: String
  },
  sessions: {
    type: [SessionSchema],
    min: 1,
    optional: false,
    custom: function() {
     var sessions = this.value; // array;
     var seatTo = 0; // initalize @ 0
     var hasError;

         // loop through seach session 
         _.each(sessions, function(s, index) {

          // if seatFrom < previous seatTo
             if (s['seat.from'] < seatTo) {
                 hasError = true;
             };
             seatTo = s['seat.to']; // update seatTo for next iteration
         }); 

         if (hasError) {
             return "greater-session"; // dispaly error on top of the session. I need to display error message on perticular field in `SessionSchema`. 
         }
    }
  }
});
于 2015-11-17T01:11:52.913 回答