Question

I'm using backbone relational with django tastypie and I have problem with validation relations.

Let's say, I have a basic model with validate method:

MyModel = Backbone.RelationalModel.extend({
   urlRoot : '/api/v1/SampleModel/',
   relations : [
   {
      type: Backbone.HasOne,
      key: 'box',
      relatedModel: 'BoxModel',
      includeInJSON: 'id'
   }],

   validate : function(attr)
   {
      if(!attr.name)
      {
         console.log('attr name validation fail');
         return "C mon! name is srsly required!"; 
      }
      if(!attr.box)
      {
        console.log('attr box validation fail');
        console.log(attr.box);
        return "Damn! you forgot to set box!";
      }
   }
});

In some view i'm creating new MyModel instance with box as resource_uri of other object:

var BoxUri = '/api/v1/Box/3'
var NewModel = new MyModel();
NewModel.set('box',BoxUri);
NewModel.set('name','new name for model');

and here is meritum... when i make save on model, it always failure on attr.box validation and attr.box is None- even when all fields are set correctly.

What's interesting, if in validate function, do something like:

validate : function(attr){ 
   if(!attr.name)
       {
          console.log('attr name validation fail');
          return "C mon! name is srsly required!"; 
       }
   console.log(attr.box);

}

In above case, attr.box is displayed in console as desired object.

Of course, if I remove validation method, object is saved correctly, with appropriate relation etc..

As I red in documentation, on default, validation is run only when save() is called, so all fields are already set.. so how (and why) validation function know, that attr.box is empty?

Or maybe my approach is simply wrong?

Thanks for any clue.

Was it helpful?

Solution 2

Probably i found solution.

modification in model definition:

MyModel = Backbone.RelationalModel.extend({
   idAttribute : 'id', //here i added declaration of id field
   urlRoot : '/api/v1/SampleModel/',
   relations : [
   {
      type: Backbone.HasOne,
      key: 'box',
      relatedModel: 'BoxModel',
      includeInJSON: 'resource_uri' // changed 'id' to 'resource_uri' probably it's not required at all
   }],

   validate : function(attr)
   {
      if(!attr.name)
      {
         console.log('attr name validation fail');
         return "C mon! name is srsly required!"; 
      }
      if(!attr.box)
      {
        console.log('attr box validation fail');
        console.log(attr.box);
        return "Damn! you forgot to set box!";
      }
   }
});

in view, i set in model just id instead resource_uri:

var BoxUri = '3'
var NewModel = new MyModel();
NewModel.set('box',BoxUri);
NewModel.set('name','new name for model');

after these two modifications, validation started working.

OTHER TIPS

You are setting the values in the wrong object :

var BoxUri = '/api/v1/Box/3'
var NewModel = new MyModel();
NewModel.set('box',BoxUri); // NewModel instead of MyModel
NewModel.set('name','new name for model'); // NewModel instead of MyModel

Your problem is elsewhere, here I setup an example that work well.

By the way, change the relatedModel: 'BoxModel', to relatedModel: BoxModel,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top