兄弟字段上的meteor autoform自定义验证消息

时间:2015-11-16 06:41:37

标签: meteor meteor-autoform simple-schema

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

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:

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

我如何实现这一目标?

2 个答案:

答案 0 :(得分:0)

我建议您使用custom validator作为SimpleSchema。他们可以访问更多上下文信息。

答案 1 :(得分: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`. 
         }
    }
  }
});