Mahipat
Mahipat

Reputation: 641

Validate backbone Model WIth Nested Object

I have on model and have some fields in that and one oject that has some more fields,and i am validate that using backbone validatation,validation working fine for name and age but for field in object(address) validation is not working.

How to validate field in address?

Ex.

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
  }
}
}
});

Thanks In advance

Upvotes: 0

Views: 381

Answers (1)

Balaji
Balaji

Reputation: 1019

In the Backbone.Validation documentation it says that :

"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'."

Hence in your case, for the validation of properties in address object it should be :

"address.pincode" : {
    required : true
}

Upvotes: 2

Related Questions