在Meteor.js中设置Autoform中的Omitted字段值

时间:2015-01-14 18:36:25

标签: javascript node.js meteor meteor-autoform

使用quickform包中的aldeed:autoform时,如何使用omitFields设置我们省略的字段的值?该字段被省略,因为我们不希望用户更改其默认值(例如:从Meteor.userId()更改userId),也不会看到该字段。

示例:

{{> quickForm collection="Contacts" id="contacts-new-form" type="insert" omitFields="avatarUrl,details.active" buttonContent="Create Contact"}}

2 个答案:

答案 0 :(得分:1)

如果您想省略字段,但为该字段提供默认值,则应使用https://github.com/aldeed/meteor-simple-schema#autovalue

中记录的autovalue

或者,您可以在架构定义中定义autoform属性,并将它们保存在一起,如下所示:

https://github.com/aldeed/meteor-autoform#putting-field-attribute-defaults-in-the-schema

如果这是一个更新表单并且您没有查找自动值,而是保留原始值,那么您可以选择将该特定字段设置为https://github.com/aldeed/meteor-collection2#problems中所述的隐藏输入字段,以便它仍然可以通过验证。

但我会使用可靠的autovalue定义。

答案 1 :(得分:0)

@Nyxynyx您需要添加此代码。只需使用hook并在插入前添加userId。请看下面的例子,我希望这会对你有帮助。

var postHooks = {
  before: {
    insert: function(doc) {
      if(Meteor.userId()){
        doc.userId = Meteor.userId();
      }
      
      return doc;
    }
  }
}