我有这个 quickForm
:
{{> quickForm id="insertFixie" collection="Fixies" type="insert" doc=seedObject}}
由这个架构支持:
Fixies = new Meteor.Collection('fixies');
Schema.Fixies = new SimpleSchema({
description: {
type: String,
label: "Description",
trim: true,
optional: true
},
cost: {
type: Number,
label: "Cost",
min: 0,
decimal: true
},
product_id: {
type: String,
autoform: {
omit: true
}
},
});
Fixies.attachSchema(Schema.Fixies);
和这个seedObject
方法:
Template.insertFixie.helpers({
seedObject: function () {
console.log({product_id: this._id});
return {product_id: this._id};
}
});
当上面的 console
调用发生时,它是正确的,并给出了一些这样的效果:
Object {product_id: "1"}
但是当我提交带有有效内容(如“stuff”和“100”)的表单时,我收到此错误:
insert error:
Error: Product is required {invalidKeys: Array[1],
validationContext: SimpleSchemaValidationContext,
stack: (...),
message: "Product is required"}
声明 product_id
属性是必需的,当前值为 null
。
我做错了什么? product_id
是一个依赖于模板的值,因此架构中的“autoValue”之类的东西似乎不是处理它的最佳方式。
文档似乎清楚地表明我使用的东西是正确的。从Auto Form
的doc
属性的描述来看:
For an insert form, you can also use this attribute to pass an object that has default form values set (the same effect as setting a value attribute on each field within the form).
并且根据afFieldInput
的value
属性的描述:
value: Set a specific, potentially reactive, value for the input. If you have also provided a doc attribute on the autoForm or quickForm, this value will override the value from the doc object.
我错过了什么?
编辑
我在我的架构中添加了一个 autoValue
字段,只是为了查看弹出的内容:
autoValue: function (doc) {
console.log(doc)
console.log(this.value)
return "1";
}
这允许表单正确提交,但使用不正确的硬编码值“1”而不是来自模板的有用值。两个 console
日志显示了这一点:
:24 Object {description: "stuff", cost: 50}
:25 undefined
我的 seedObject
值似乎不适用于 autoValue。
我是否必须劫持 onSubmit
Hook ?我是否必须使用模板提供的值隐藏表单输入?这里的修复是什么?
最佳答案
原来是隐藏输入。
我将我的表单扩展为:
{{#autoForm id="insertFixie" collection="Fixies" type="insert"}}
<fieldset>
{{> afFormGroup name="description" placeholder="schemaLabel" label=false}}
<div class="form-group{{#if afFieldIsInvalid name='cost'}} has-error{{/if}}">
<div class="input-group">
<div class="input-group-addon">$</div>
{{> afFieldInput name="cost" placeholder="schemaLabel" label=false}}
</div>
{{#if afFieldIsInvalid name="cost"}}
<span class="help-block">{{afFieldMessage name="cost"}}</span>
{{/if}}
</div>
{{> afFormGroup name="product_id" type="hidden" value=_id}}
</fieldset>
<button class="btn btn-primary" type="submit">Insert</button>
{{/autoForm}}
使用 type="hidden"
添加一个 afFormGroup
就可以了。
尽管在我看来 doc
论点仍然没有兑现它的 promise 。
关于javascript - 自动成型插入 : doc value isn't having any effect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29417889/