使用嵌套对象验证 backbone 模型

Validate backbone Model WIth Nested Object

我有一个模型,里面有一些字段,还有一个对象有更多字段,我正在使用 backbone 验证来验证,验证对姓名和年龄工作正常,但对对象中的字段有效(地址)验证无效。

如何验证地址字段?

例如

var PersonModel = Backbone.Model.extend({
defaults: {
name: null,//String
age: null,//Integer
address: {
   pincode : null,
   streetName : ''
}
}
validation: {
name: {
required: true
},
age: {
range: [1, 80]
},
address : {
  pincode :{
    required : true
  }
}
}
});

提前致谢

Backbone.Validation documentation 中说:

"Validating complex objects is also supported. To configure validation rules for objects, use dot notation in the name of the attribute, e.g 'address.street'."

因此,在您的情况下,为了验证地址对象中的属性,它应该是:

"address.pincode" : {
    required : true
}