2015-11-16 54 views
0

如何将自定义验证消息发送到另一个模式字段?流星autoform自定义验证消息在兄弟字段

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}} 

如何实现这一目标?

回答

0

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

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`. 
     } 
    } 
    } 
});