Meteor 数组中的自动形成引用特定字段

Meteor 数组中的自动形成引用特定字段,meteor,meteor-autoform,simple-schema,Meteor,Meteor Autoform,Simple Schema,我正在尝试使用自动表单更新集合中的数组对象。该集合包含大量信息,但是使用此表单,我只想更新careerHistory。我希望能够使用引导列控制表单的布局。为此,我需要能够独立引用careerHistory.$.company和careerHistory.$.title。目前,我只能通过引用name=“careerHistory”来呈现表单。每当我尝试引用数组中的特定字段时,表单都不会打印 路径:Profile.js import { Mongo } from 'meteor/mongo'; im

我正在尝试使用自动表单更新集合中的数组对象。该集合包含大量信息,但是使用此表单,我只想更新careerHistory。我希望能够使用引导列控制表单的布局。为此,我需要能够独立引用
careerHistory.$.company
careerHistory.$.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() {
  return Profile.findOne({userId: Meteor.userId()});
},
profileCollection() {
  return Profile;
}
路径:
Profile.html

{{#autoForm collection=profileCollection doc=profile id="candidateCareerHistory" type="update"}}
  {{> afArrayField name="careerHistory"}}
{{/autoForm}}
路径:
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() {
  return Profile.findOne({userId: Meteor.userId()});
},
profileCollection() {
  return Profile;
}

如果使用块帮助器并为所要查找的特定字段生成图表,则可以执行此操作。这里有一个例子

{{#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}}
{{{#autoForm collection=Profile doc=Profile id=“candidateCareerHistory”type=“update pushArray”scope=“careerHistory”}
{{#如果附加了有效名称=“careerHistory”}
{{{afFieldMessage name=“careerHistory”}}}
{{/if}
{{{#afEachArrayItem name=“careerHistory”}
{{>afFieldInput name=this.current.company}
{{>afFieldInput name=this.current.title}
{{/afEachArrayItem}
{{/autoForm}
现在,您可以使用所需的任何机制设置字段的样式。注意,以这种方式构建表单时,您必须添加自己的“添加”和“删除”按钮。我已经包括了上面的默认添加/删除


有关完整的引导样式示例,请参阅。

如果您使用块帮助器并为所需的特定字段生成图表,则可以执行此操作。这里有一个例子

{{#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}}
{{{#autoForm collection=Profile doc=Profile id=“candidateCareerHistory”type=“update pushArray”scope=“careerHistory”}
{{#如果附加了有效名称=“careerHistory”}
{{{afFieldMessage name=“careerHistory”}}}
{{/if}
{{{#afEachArrayItem name=“careerHistory”}
{{>afFieldInput name=this.current.company}
{{>afFieldInput name=this.current.title}
{{/afEachArrayItem}
{{/autoForm}
现在,您可以使用所需的任何机制设置字段的样式。注意,以这种方式构建表单时,您必须添加自己的“添加”和“删除”按钮。我已经包括了上面的默认添加/删除


有关完整的引导样式示例,请参阅。

谢谢@jordanwillis。我找到的唯一解决方案是创建一个自定义模板,然而,这看起来非常混乱。你的解决方案更干净。它唯一缺少的是错误消息。如何获取要显示的错误消息?我试过
{{{{if-affeldisinvalid name=“careerHistory”}{{{{affeldmessage name=“careerHistory”}}}{{/if}}}
但不起作用,应该可以。您是否将其添加到正确的位置(在
afEachArrayItem
块之外)?看看我最新的答案。我想我可能做了一些根本错误的事情。我使用的是新的npm简单模式和autoform 6.0,我创建的任何表单都没有得到任何验证。我是否应该向架构中添加一些内容?您是否也安装了collection2 core?是吗?我已经更新了上面的架构。我正在使用collection2 core,不确定如何让它返回错误。在我以前的meteor应用程序中,我遗漏了什么?这很有效。谢谢@jordanwillis。我找到的唯一解决方案是创建一个自定义模板,然而,这看起来非常混乱。你的解决方案更干净。它唯一缺少的是错误消息。如何获取要显示的错误消息?我试过
{{{{if-affeldisinvalid name=“careerHistory”}{{{{affeldmessage name=“careerHistory”}}}{{/if}}}
但不起作用,应该可以。您是否将其添加到正确的位置(在
afEachArrayItem
块之外)?看看我最新的答案。我想我可能做了一些根本错误的事情。我使用的是新的npm简单模式和autoform 6.0,我创建的任何表单都没有得到任何验证。我是否应该向架构中添加一些内容?您是否也安装了collection2 core?是吗?我已经更新了上面的架构。我正在使用collection2 core,不确定如何让它返回错误。我错过了什么,在我以前的meteor应用程序中,这起作用了。