使用autoform for meteor在客户端/服务器上操作字段内容

时间:2015-04-21 20:29:40

标签: javascript meteor meteor-autoform

我正在玩Meteor和autoform。

我的设置

我有一个输入字段(类型"时间")作为我的模式的一部分,用于收集"歌曲"强制用户键入语法 [num] [num]:[num] [num] 。 这是我的架构:

time: {
    type: Number,
    label: "Time",
    optional: true,
    autoform: {
        afFieldInput: {
            type: 'time'
        }
    }
}

我想做什么

点击"提交"但验证前我想将字符串(例如" 03:45")转换为秒(数字),以便验证通过时没有错误。

另外:当从数据库中读取数据时,我希望将其转换回字符串,以便它作为值适合输入字段。

我在autoform,collection2或simple-schema的文档中找不到答案(或者至少不理解它; - )

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

使用autoform hooks。您要查找的信息如下: https://github.com/aldeed/meteor-autoform#callbackshooks

更具体地说,您正在寻找的是:

AutoForm.hooks({
  someFormId: {
    formToDoc: function(doc) {
        // Called every time an insert or typeless form
        // is revalidated, which can be often if keyup
        // validation is used.
    },
    docToForm: function(doc, ss) {
        // Called whenever `doc` attribute reactively changes, before values
        // are set in the form fields.
    }
});

可以在每个钩子中修改doc,特别是当它到达集合时以及从集合中被反应性地拉出时。