"in" does not validate properly
donaldducky opened this issue · 7 comments
I have a model with validations like
type:
in: ['a','b']
When I save with
model.save({ type: 'a' })
The 'in' validator doesn't get passed valueToSet
properly:
"in" : function(whitelist, attributeName, model, valueToSet) {
console.log(whitelist, attributeName, model, valueToSet);
return _.include(whitelist, valueToSet) ? undefined : "in";
},
Gives the log output:
["a", "b"]
"type"
d
undefined
valueToSet
is undefined somewhere along the way.
My quick fix, for now:
"in" : function(whitelist, attributeName, model, valueToSet) {
valueToSet = model.get(attributeName);
return _.include(whitelist, valueToSet) ? undefined : "in";
},
Nevermind! I was using it wrong.
When I had it passing nothing into save:
model.save({ }, { ... });
I guess validations aren't run on what's already set, only what's passed in on save?
If you set it with {silent: true} the validations won't be run.
Can you post more code? I'm curious about what you're doing.
I had something setup like this:
var MyModel = Backbone.Model.extend({
url: 'test',
validate: {
type: {
in : [ 'a', 'b' ]
}
}
});
var b = new MyModel({ type: 'b' });
b.save({}, {
success: function(model, response){
console.log('success');
},
error: function(model, response){
console.log('error', model, response);
}
});
I was expecting it to pass validation since the type is "in" the list.
Setting it up like this triggers the save call to the server.
var MyModel = Backbone.Model.extend({
url: 'test',
validate: function(attributes) {
if (_.include(['a','b'], attributes.type)) {
return 'no a';
}
}
});
var a = new MyModel({ type: 'b' });
a.save({}, {
success: function(model, response){
console.log('success');
},
error: function(model, response){
console.log('error', model, response);
}
});
So, when using the validations plugin, it doesn't seem to trigger the same way as backbone's, in this case.
Let me know if you need more info!
Cool. That's bad, so I'm going to reopen this.
Closing this issue in favour of the exact problem at #12