我有这个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"}但是,当我提交带有有效内容的表单(如“内容”和"100")时,我会得到以下错误:
insert error:
Error: Product is required {invalidKeys: Array[1],
validationContext: SimpleSchemaValidationContext,
stack: (...),
message: "Product is required"}声明product_id属性是必需的,并且当前的值为null。
我做错了什么?product_id是一个依赖于模板的值,所以模式中的"autoValue“这样的东西似乎不是处理它的最佳方法。
医生们似乎清楚地表明,我使用的东西是正确的。从对doc属性Auto Form的描述
对于insert表单,还可以使用此属性传递具有默认窗体值集的对象(与在窗体中的每个字段上设置值属性相同)。
并通过对value属性afFieldInput的描述
value:为输入设置一个特定的、可能是反应性的值。如果您还在autoForm或quickForm上提供了doc属性,则该值将覆盖来自doc对象的值。
我遗漏了什么?
编辑
我在我的模式中添加了一个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钩吗?我是否必须使用模板提供的值来隐藏表单输入?解决办法是什么?
发布于 2015-04-03 03:43:04
结果是一种隐藏的输入。
我把我的表格扩展到:
{{#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}}在afFormGroup中添加一个type="hidden"就能达到这个目的。
虽然在我看来,doc的论点仍然不符合它的承诺。
https://stackoverflow.com/questions/29417889
复制相似问题