backbone.validation icon indicating copy to clipboard operation
backbone.validation copied to clipboard

Error when validating model with two equal array names

Open thomastvedt opened this issue 9 years ago • 2 comments

My model looks like this: Activity

  • Id
  • CategoryIds []
  • Name
  • Task
    • Id
    • CategoryIds['vacation']

When I call model.validate(), I get the following error: Uncaught TypeError: Cannot use 'in' operator to search for '0' in vacation

Is this because I have two attributes named "CategoryIds" in my model? One attribute "activity.CategoryIds[]" and one nested attribute "activity.Task.CategoryIds[]". Maybe flattening isn't working? Is this a bug, and can it be fixed? :-) Or am I doing something wrong? (validation works on simpler models).

trace: Uncaught TypeError: Cannot use 'in' operator to search for '0' in vacationb.each.b.forEach @ underscore-min.js:20Backbone.Validation.flatten @ backbone-validation.js:82(anonymous function) @ backbone-validation.js:86b.each.b.forEach @ underscore-min.js:20(anonymous function) @ backbone-validation.js:85b.each.b.forEach @ underscore-min.js:20Backbone.Validation.flatten @ backbone-validation.js:82(anonymous function) @ backbone-validation.js:90b.each.b.forEach @ underscore-min.js:20Backbone.Validation.flatten @ backbone-validation.js:82Backbone.Validation.options.validate @ backbone-validation.js:285Page.extend.save @ ActivityEditor2.js:95jQuery.event.dispatch @ jquery-1.9.1.js:3074jQuery.event.add.elemData.handle @ jquery-1.9.1.js:2750

thomastvedt avatar Sep 18 '15 10:09 thomastvedt

Can you please include your model definition including its validation block? On Sep 18, 2015 5:46 AM, "Thomas Tvedt" [email protected] wrote:

My model looks like this: Activity

  • Id
  • CategoryIds []
  • Name
  • Task
    • Id
    • CategoryIds['vacation']

When I call model.validate(), I get the following error: Uncaught TypeError: Cannot use 'in' operator to search for '0' in vacation

Is this because I have two attributes named "CategoryIds" in my model? One attribute "activity.CategoryIds[]" and one nested attribute "activity.Task.CategoryIds[]". Maybe flattening isn't working? Is this a bug, and can it be fixed? :-) Or am I doing something wrong? (validation works on simpler models).

trace: Uncaught TypeError: Cannot use 'in' operator to search for '0' in vacationb.each.b.forEach @ underscore-min.js:20Backbone.Validation.flatten @ backbone-validation.js:82(anonymous function) @ backbone-validation.js:86b.each.b.forEach @ underscore-min.js:20(anonymous function) @ backbone-validation.js:85b.each.b.forEach @ underscore-min.js:20Backbone.Validation.flatten @ backbone-validation.js:82(anonymous function) @ backbone-validation.js:90b.each.b.forEach @ underscore-min.js:20Backbone.Validation.flatten @ backbone-validation.js:82Backbone.Validation.options.validate @ backbone-validation.js:285Page.extend.save @ ActivityEditor2.js:95jQuery.event.dispatch @ jquery-1.9.1.js:3074jQuery.event.add.elemData.handle @ jquery-1.9.1.js:2750

— Reply to this email directly or view it on GitHub https://github.com/thedersen/backbone.validation/issues/309.

platinumazure avatar Sep 18 '15 12:09 platinumazure

The error appears even though there is no validation-block defined. (when I call model.set(), which calls .validate() etc.)

This is what my Activity-model looks like while debugging (js-code is minimal):

activityandtasksameid

it fails in "flatten(obj, into, prefix) backbone-validation.js:78

var flatten = function (obj, into, prefix) {
into = into || {};
prefix = prefix || '';

_.each(obj, function(val, key) {
  if(obj.hasOwnProperty(key)) {
    if (!!val && _.isArray(val)) {
      _.forEach(val, function(v, k) {
        flatten(v, into, prefix + key + '.' + k + '.');
        into[prefix + key + '.' + k] = v;
      });
    } else if (!!val && typeof val === 'object' && val.constructor === Object) {
      flatten(val, into, prefix + key + '.');
    }

    // Register the current level object as well
    into[prefix + key] = val;
  }
});

return into;
};

When flattening Task.CategoryIds, _.isArray(key) is true.. then there is a recursive call to: flatten(obj="con", into=.., prefix="Task.CategoryIds.0."){

now on line 82 it fails, because obj is a string and not an array. obj = "con": _.each(obj, function(val, key) {

Uncaught TypeError: Cannot use 'in' operator to search for '0' in con

Activity.Task.CategoryIds is an array of strings. Is there something wrong with my model?

thomastvedt avatar Sep 22 '15 09:09 thomastvedt