使用匹配输入的backbone-forms插件错误

时间:2012-05-21 02:19:18

标签: javascript backbone.js

我正在尝试验证表单。当我指定默认数据来填充不匹配的表单时,而不是通过commit验证,我预计会记录错误(这是正确的):

Errors: {"confirmedUsername":{"type":"match","message":"Usernames must match!"}}

JsFiddle:http://jsfiddle.net/franklovecchio/FkNwG/173/

当我指定匹配的默认数据时,通过commit验证,我得到一个实际的控制台错误:

Uncaught TypeError: Cannot call method 'set' of undefined

JsFiddle:http://jsfiddle.net/franklovecchio/FkNwG/172/

是什么给出的?

1 个答案:

答案 0 :(得分:0)

弗兰克从文档中看来你做错了。

JSFiddle(工作)

http://jsfiddle.net/FkNwG/176/

原因

您可以创建表单而不将其绑定到模型。例如,要为简单的数据对象创建表单:

var form = new Backbone.Form({
    data: { id: 123, name: 'Rod Kimble', password: 'cool beans' }, //Data to populate the form with
    schema: {
        id:         { type: 'Number' },
        name:       {},
        password:   { type: 'Password' }
    }
}).render();

然后代替form.commit(),执行:

var data = form.getValue(); //Returns object with new form values

我刚刚将commit更改为getvalue()