【问题标题】:meteor autoform custom validation message on sibling fields同级字段上的流星自动生成自定义验证消息
【发布时间】:2015-11-17 11:50:39
【问题描述】:

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

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

我如何做到这一点?

【问题讨论】:

    标签: meteor meteor-autoform simple-schema


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我会尝试这样的:

      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-24
        • 2015-10-31
        • 1970-01-01
        • 2014-09-27
        • 2015-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-12
        相关资源
        最近更新 更多