meteorjs minimongoid + autoform

时间:2014-07-11 16:08:12

标签: meteor

我试图让minimalongoid与meteorjs合作,但我得到一个错误声明:

Error: AutoForm: collection attribute for form with id "insertBuildingForm" is not a Meteor.Collection instance

这是否意味着minimalongoid集合不被视为Meteor集合而且我不能让它与autoform一起玩?或者我做错了什么?

如果minimongoid和autoform不兼容,使用minimongoid管理验证的最佳方法是什么?我的意思是自动...; - )

THX。

2 个答案:

答案 0 :(得分:1)

我得到了它,使用它:

编辑:随着collection2 2.0的发布,API已经改变,你必须使用attachSchema即

class @Book extends Minimongoid

    @_collection:  new Meteor.Collection('books')

Book._collection.attachSchema new SimpleSchema({
    name:
      type: String
      label: "Name"
      max: 200
    author:
      type: String
      label: "Author"
    publishedDate:
      type: Date
      label: "Publication Date"
    price: 
      type: Number
      label: "Price" 
      min: 0
})

然后在html模板中

{{> quickForm collection="Book._collection" id="insertBookForm" type="insert"}}

请注意,该集合应为Book._collection

答案 1 :(得分:0)

是的,这意味着minimongoid集合不兼容,除非它是Meteor.Collection的扩展。

执行此操作的行在此处:https://github.com/aldeed/meteor-autoform/blob/e77832d40f8eec2ddb92d97f481bf86fb712d9ca/utility.js#L261-L263

很难说如何使用minimongoid来实现它,因为它不是核心包。它确实支持验证。

validate: ->
  unless @name and @name.length > 3
    @error('name', 'Recipe name is required and should be longer than 3 letters.')

它支持验证的方式是通过Collection.insert规则(我认为?)。因此,在您尝试插入文档时,您将收到此错误,类似于删除insecure包时“拒绝访问”错误的方式。

还有客户端验证,您必须通过例如errors

检查Recipe.errors.length属性