【问题标题】:Autoform reference specific field within array在数组中自动生成引用特定字段
【发布时间】:2017-03-22 07:28:58
【问题描述】:

我正在尝试使用自动表单来更新集合中的数组对象。该集合包含大量信息,但是使用此表格我只想更新职业历史。我希望能够使用引导列控制表单的布局。为此,我需要能够独立引用careerHistory.$.companycareerHistory.$.title。目前,我只能通过引用name="careerHistory" 来呈现表单。每当我尝试引用数组中的特定字段时,表单都不会打印。

路径:Profile.js

import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
import { addressSchema } from '../../sharedSchemas/addressSchema.js';

SimpleSchema.extendOptions(['autoform']);

export const Profile = new Mongo.Collection('Profile');

Profile.allow({
  insert: function(userId, doc) {
    return !!userId;
  },
  update: function(userId, doc) {
    return !!userId;
  },
  remove: function(userId, doc) {
    return !!userId;
  }
});    

Schemas.Profile = new SimpleSchema({
      userId: {
        type: String,
        optional: true
      },
      'careerHistory.$': Object,
      'careerHistory.$.company': {
        type: String,
        optional: false,
      },
      'careerHistory.$.title': {
        type: String,
        optional: true,
    });

Profile.attachSchema(Schemas.Profile);

路径:Profile.html

{{#autoForm collection=profileCollection doc=profile id="candidateCareerHistory" type="update"}}
  {{> afArrayField name="careerHistory"}}
{{/autoForm}}

路径:Profile.js

profile() {
  return Profile.findOne({userId: Meteor.userId()});
},
profileCollection() {
  return Profile;
}

【问题讨论】:

    标签: meteor meteor-autoform simple-schema


    【解决方案1】:

    如果您使用afEachArrayItem 块助手并为您所追求的特定字段构建图表,则可以执行此操作。这是一个例子。

    {{#autoForm collection=Profile doc=profile id="candidateCareerHistory" type="update-pushArray" scope="careerHistory"}}
      {{#if afFieldIsInvalid name="careerHistory"}}
        <div class="panel-body has-error">
          <span class="help-block">{{{afFieldMessage name="careerHistory"}}}</span>
        </div>
      {{/if}}
    
      {{#afEachArrayItem name="careerHistory"}}
        <button type="button" class="btn btn-primary autoform-remove-item"><span class="glyphicon glyphicon-minus"></span></button>
        {{> afFieldInput name=this.current.company}}  
        {{> afFieldInput name=this.current.title}}
      {{/afEachArrayItem}}
      <button type="button" class="btn btn-primary autoform-add-item" data-autoform-field="careerHistory"><span class="glyphicon glyphicon-plus"></span></button>
    {{/autoForm}}
    

    现在您可以使用任何您想要的机制来设置字段的样式。请注意,当您以这种方式构建表单时,您必须添加自己的“添加”和“删除”按钮。我已经包含了上面的默认添加/删除。

    请参阅default template 以获取完整的引导样式示例。

    【讨论】:

    • 谢谢@jordanwillis。我找到的唯一解决方案是创建一个自定义模板,但是,这看起来真的很乱。您的解决方案更清洁。它唯一缺少的是错误消息。您如何获得要显示的错误消息?我试过{{#if afFieldIsInvalid name="careerHistory"}} &lt;div class="panel-body has-error"&gt; &lt;span class="help-block"&gt;{{{afFieldMessage name="careerHistory"}}}&lt;/span&gt; &lt;/div&gt; {{/if}},但它不起作用。
    • 应该可以。您是否将其添加到正确的位置(afEachArrayItem 块之外)?请参阅我的更新答案。
    • 我想我可能做错了什么。我正在使用新的 npm simple-schema 和 autoform 6.0,但我创建的任何表单都没有得到验证。我想在架构中添加一些东西吗?
    • 你也安装了collection2-core吗?attach your schema to the collection
    • 我已经更新了上面的架构。我正在使用collection2-core,不确定如何让它返回错误。我错过了什么,在我的旧流星应用程序中这有效。
    猜你喜欢
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多