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

Invalide model saves

Open salarmehr opened this issue 11 years ago • 6 comments

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

salarmehr avatar Nov 28 '13 04:11 salarmehr

+1

trsrm avatar Feb 10 '14 11:02 trsrm

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 :)

sharshenov avatar Feb 14 '14 08:02 sharshenov

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

salarmehr avatar Feb 14 '14 08:02 salarmehr

Well, it definitely is a bug. What if my validation rules and fields are defined dynamically?

kix avatar Oct 14 '14 17:10 kix

@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

kix avatar Oct 14 '14 17:10 kix

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/

chiefGui avatar Feb 03 '15 14:02 chiefGui