backbone.validation
backbone.validation copied to clipboard
Error when validating model with two equal array names
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
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.
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):
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?