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

isNew and changed attrs #130

Open
andriijas opened this issue May 13, 2013 · 9 comments
Open

isNew and changed attrs #130

andriijas opened this issue May 13, 2013 · 9 comments
Labels

Comments

@andriijas
Copy link

Hi

When the validation is running on a new model with unchanged attributes the validation isnt applied correctly.

When I debug validate i can see all correct errors in result.invalidAttrs but because changedAttrs is empty because the model is brand new and not changed the validation slips through and the model is saved to the server anyway

Backbone 1.0

@cmwelsh
Copy link

cmwelsh commented Jun 29, 2013

I am seeing this as well. It comes down to this check here:

https://github.com/thedersen/backbone.validation/blob/master/dist/backbone-validation-amd.js#L263

I am going to have to figure out a workaround and report back.

@cmwelsh
Copy link

cmwelsh commented Jun 29, 2013

I decided to just hack this part out:

&& _.intersection(_.keys(result.invalidAttrs), _.keys(changedAttrs)).length > 0

@bgaillard
Copy link

Hi, we also encounter problems with this peace of code && _.intersection(_.keys(result.invalidAttrs), _.keys(changedAttrs)).length > 0.

In our case it seems to be the same problem as issue #220.

We need a fix for this, is it possible to apply @cmwelsh's fix (&& _.size(result.invalidAttrs) > 0) quickly ?

@bgaillard
Copy link

Tried to apply @cmwelsh's but unit test fails, so its not a good fix.

In our case the problem is encountered when we validate a model where attributes are not defined.

var ModelClass = Backbone.Model.extend({
    validation : {
        name : {
            required : true
        }
    }
});

...

var model = new ModelClass();
if(model.save() === false) {
    // We should enter here but the _intersection instruction prevent it
}

As a workaround we simply define default values in our model classes.

var ModelClass = Backbone.Model.extend({
    defaults : {
        name : undefined
    },
    validation : {
        name : {
            required : true
        }
    }
})

...

var model = new ModelClass();
if(model.save() === false) {
    // Now we should enter here
}

Hope this helps.

@chiefGui
Copy link
Collaborator

chiefGui commented Feb 3, 2015

@bgaillard Your fix is good when your attributes aren't dynamic. Anyway, this is a known-issue and we're working on it. Also reported by #188.

@chiefGui chiefGui added the bug label Feb 3, 2015
@bgaillard
Copy link

Hi @chiefGui, I didn't remember I posted this :-) Happy to see the library is regularly updated now, we use it in several projects and it helps us a lot, thanks.

@chiefGui
Copy link
Collaborator

chiefGui commented Feb 4, 2015

Hey @bgaillard!

I'm glad to know that you use it in your projects. Lots of things are about to come — stay in sync! 😄

@chk-
Copy link

chk- commented Jul 24, 2015

What about changing the line
if (!opt.forceUpdate && _.intersection(_.keys(result.invalidAttrs), _.keys(changedAttrs)).length > 0) {
to this:
if (!opt.forceUpdate && _.intersection(_.keys(result.invalidAttrs), _.keys(validatedAttrs)).length > 0) {
?
So I did and I'm testing it in my app. The validatedAttrs stands in direct relation to invalidAttrs and this way I don't have to define "extra" defaults on the model.

@platinumazure
Copy link

@chk- Feel free to write up a pull request if you think that approach will work!

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

6 participants