【问题标题】:meteor autoform required validation not working with nested schema流星自动生成需要验证不适用于嵌套模式
【发布时间】:2015-12-18 19:03:54
【问题描述】:

下面是我的嵌套架构。 name.first 是必需的。但是当我提交它没有验证的表单时,它允许空字符串。我错过了什么吗?或者如何解决这个问题?

    Schema.UserProfile = new SimpleSchema({
      'name.first': {
        type: String,
        max: 50,
        label: "First name"
      },
      'name.last': {
        type: String,
        optional: true,
        max: 50,
        label: "Last name"
      }
    });

    Schema.User = new SimpleSchema({
     profile: {
        type: Schema.UserProfile,
        optional: true
      },
    });

Meteor.users.attachSchema(Schema.User);

表格:

{{#autoForm id="profile" type="method-update" meteormethod="updateProfile" schema=userSchema doc=currentUser collection=Users}}
        {{> afQuickField name="profile.name.first" autofocus='' formgroup-class="col-xs-6"}}
        {{> afQuickField name="profile.name.last" formgroup-class="col-xs-6"}}
{{/autoForm}}

【问题讨论】:

  • 您的架构和表单看起来正确。我将它复制/粘贴到一个小项目中,它按预期工作。清除名字字段会显示消息“需要名字”。 profile.name.first 字段是否真的为空,您是否在 mongoDB 中检查它?你有一个模板助手Users,它返回 Meteor.users 对吗?
  • 谢谢@JosHarink。我在Userstemplatehelper 中犯了一个错误。非常感谢
  • @JosHarink 您是否尝试过在不输入任何数据的情况下提交?
  • 不,我坚持你的问题,检查了架构是否经过验证,仅此而已。我做了一个假的updateProfile 方法。为什么?

标签: meteor meteor-autoform


【解决方案1】:

我已经检查了你的架构 架构看起来不错,但您需要为名称添加对象。

Schema.UserProfile = new SimpleSchema({
  'name': {
   type: Object,
   optional: false
  },
  'name.first': {
    type: String,
    max: 50,
    label: "First name"
  },
  'name.last': {
    type: String,
    optional: true,
    max: 50,
    label: "Last name"
  }
});

【讨论】:

    猜你喜欢
    • 2015-11-24
    • 1970-01-01
    • 2014-12-14
    • 2020-06-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    相关资源
    最近更新 更多