一直在努力用自动表格来预填充数据,特别是在隐藏字段中。
我尝试了很多事情,包括使用autoValue和defaultValue,但是autoValue在服务器端被验证了,我需要从客户端的页面(路由器当前的路由名称)获取一个值,所以当在.clean函数中查找它时,它会失败,而defaultValue会得到一个值,并且不会接受一个函数。
如何在不显示字段的情况下,将值传递给表单以预填充某些字段?
发布于 2015-09-19 09:42:44
所以,我发布了这个问题,因为我一直在挣扎,找到了答案,并想要分享。
最后,您可以将一个doc
属性以如下形式传递给表单:
{{> quickform
collection"mycollection"
id="formid"
type="method"
...
doc=mydoc
}}
然后需要一个模板助手来创建文档:
Template.myform_template.helper({
mydoc: function() {
return {field1: value1, field2:value2 };
}
})
您不必填充所有字段,只需要预先填充字段,就像“更新”表单的工作方式一样。
为了不在表单中显示该值,我尝试使用omitFields
属性,但这在删除文档中的字段时不起作用。因此,我发现的唯一方法是将模式中的字段类型声明为“隐藏”
mycollection.attachSchema(new SimpleSchema({
field1: {
type: String,
optional: false,
autoform: {
type: "hidden"
}
}
}))
而你却在这里。
现在,在方法中调用schema.clean(doc)时,该值并不是真正的“验证”,因为在模式中没有什么可验证的,因此您应该在方法调用中自己验证这些值。
发布于 2015-09-19 10:26:15
当我需要一个atoform的预填充字段时,我使用AutoFom.hooks,而在template.html文件中我不添加这些字段。
TemplateFile.html
{{#autoForm schema="Schemas.Myschema" id="idForm" resetOnSuccess="true" type="method" meteormethod="server/insertCustomizedTemplate"}}
{{> afQuickField name='Field1'}}
{{> afQuickField name='Field2'}}
{{> afQuickField name='Field3'}}
<button href="" type="submit" class="btn btn-success">
Send
</button>
{{/autoForm}}
TemplateFile.js
AutoForm.hooks({
idForm: {
before: {
method: function(doc) {
doc.Dummyfield1 = 'Harcoded Value';
doc.Dummyfield2 = 'Harcoded Value';
return doc;
}
},
onSuccess: function(formType, result) {
},
onError: function(formType, error) {
}
}
});
https://stackoverflow.com/questions/32671226
复制相似问题