Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalide model saves #188

Open
salarmehr opened this issue Nov 28, 2013 · 6 comments
Open

Invalide model saves #188

salarmehr opened this issue Nov 28, 2013 · 6 comments
Labels

Comments

@salarmehr
Copy link

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
@trsrm
Copy link

trsrm commented Feb 10, 2014

+1

@sharshenov
Copy link

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

@salarmehr
Copy link
Author

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

@kix
Copy link

kix commented Oct 14, 2014

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

@kix
Copy link

kix commented Oct 14, 2014

@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

@chiefGui
Copy link
Collaborator

chiefGui commented Feb 3, 2015

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants