【问题标题】:meteor autoform _id field流星自动生成_id字段
【发布时间】:2015-06-01 21:13:54
【问题描述】:

我有这个架构:

Schemas = {};

Schemas.Id = {
    type: String,
    optional: true,
    label: 'Id',
    //max: ID_LENGTH,
    defaultValue : Random.id(ID_LENGTH),
    denyUpdate: true
};

Schemas.Name = {
    type: String,
    label: 'Name',
    max: 75
};

Schemas.Description = {
    type: String,
    label: 'Description',
    max: 500,
    optional: true,
    autoform: {
        rows: 5
    }
};

//-- Attribute
Schemas.Attribute = {
    _id: Schemas.Id,
    attribute_name : Schemas.Name,
    attribute_description : Schemas.Description
};

Collections.Attributes.attachSchema(Schemas.Attribute);

我有一个客户的请求,他们可以手动插入 _id。 但autoform不允许, 这是模板:

<template name="pg_attr_insert">
    {{#autoForm _id="afInsertDemo" type="insert" collection=Collections.Attributes}}
        <div class="form-group {{#if afFieldIsInvalid name='_id'}}has-error{{/if}}">
            <label class="control-label">{{afFieldLabelText name='_id'}}</label>
            {{> afFieldInput name='_id'}}
            {{#if afFieldIsInvalid name='_id'}}
                <span class="help-block">{{{afFieldMessattribute_description name='_id'}}}</span>
            {{/if}}
        </div>
        <div class="form-group {{#if afFieldIsInvalid name='attribute_name'}}has-error{{/if}}">
            <label class="control-label">{{afFieldLabelText name='attribute_name'}}</label>
            {{> afFieldInput name='attribute_name'}}
            {{#if afFieldIsInvalid name='attribute_name'}}
                <span class="help-block">{{{afFieldMessattribute_description name='attribute_name'}}}</span>
            {{/if}}
        </div>
        <div class="form-group {{#if afFieldIsInvalid name='attribute_description'}}has-error{{/if}}">
            <label class="control-label">{{afFieldLabelText name='attribute_description'}}</label>
            {{> afFieldInput name='attribute_description'}}
            {{#if afFieldIsInvalid name='attribute_description'}}
                <span class="help-block">{{{afFieldMessattribute_description name='attribute_description'}}}</span>
            {{/if}}
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary">Add Person</button>
            <button type="reset" class="btn btn-default">Reset Form</button>
        </div>
    {{/autoForm}}
</template>

这是错误:

未捕获的错误:每个 autoForm 和 quickForm 都必须有一个“id” 属性设置为唯一的字符串。

是否有 autoform 允许手动插入 _id?以及如何?

【问题讨论】:

  • 您可以创建自己的 ID 字段以显示并保持 _id 字段对用户隐藏。
  • 我试过了,但是提交的时候没有用,我的意思是它收到错误并且没有插入数据

标签: meteor meteor-autoform


【解决方案1】:

好吧,我知道我的错误,我创造了

{{#autoForm _id="afInsertDemo" type="insert" collection=Collections.Attributes}}

我只是将其转换为:

{{#autoForm id="afInsertDemo" type="insert" collection=Collections.Attributes}}

【讨论】:

  • 正确。您在模式中定义的 id 用于将插入到 mongo 集合中的唯一记录 ID。您收到的错误消息是因为表单没有与之关联的 ID。将其视为&lt;form id="formID"&gt;,在自动格式中写为{{#autoForm id="formID" .... }}
猜你喜欢
  • 1970-01-01
  • 2015-07-25
  • 2018-03-21
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-15
  • 1970-01-01
相关资源
最近更新 更多