backbone.validation
backbone.validation copied to clipboard
Invalide model saves
Model proceed to save while form is not valid. What is wrong?
_.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
M=Backbone.Model.extend({
validation:{
name:{
required:true
}
},
url:'foo'
})
m=new M();
m.validate(); //return correct validation error.
m.validationError; // this is null while it should be filled by above error
m.save(); // it communicate with server while the model is not valid
+1
It is not a bug.
It happens because backbone calls validate
method with all model attributes. Here the code:
https://github.com/jashkenas/backbone/blob/master/backbone.js#L569
Here Backbone.Validation checks for attributes: https://github.com/thedersen/backbone.validation/blob/master/dist/backbone-validation.js#L232
Your model has no attributes at all. So Backbone.Validation checks for nothing. You can define model's attributes defaults to get validation work as you expect.
M=Backbone.Model.extend({
defaults:{
name: ''
}
validation:{
name:{
required:true
}
},
url:'foo'
})
Looks like this issue should be mentioned in ReadMe/wiki :)
When I define some validation rule this mean that my model has such attribute. I think the plugin should not be depend on default values. Some times there is no default value
Well, it definitely is a bug. What if my validation rules and fields are defined dynamically?
@sharshenov, your comment went outdated. You should've linked to a line in a commit, like this: https://github.com/jashkenas/backbone/blob/1dbe3ddf2f757b0f1a23e22ca5986b6e4a3fda90/backbone.js#L35
Yes, it is a bug. I don't know how it took too long to get attention. Working on it.
Diagnostics: http://jsfiddle.net/z17boh0h/