Skip to content

Commit

Permalink
Fixed removing invalid class in view when validating dependent attrib…
Browse files Browse the repository at this point in the history
…utes, and changing one makes the other valid
  • Loading branch information
thedersen committed Feb 23, 2012
1 parent d5f8d14 commit 4589f26
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
6 changes: 4 additions & 2 deletions backbone.validation.js
Expand Up @@ -65,7 +65,7 @@ Backbone.Validation = (function(Backbone, _, undefined) {
}, '');
};

var validateAll = function(model, validation, attrs, computed) {
var validateAll = function(model, validation, attrs, computed, view, options) {
if (!attrs) {
return false;
}
Expand All @@ -75,6 +75,8 @@ Backbone.Validation = (function(Backbone, _, undefined) {
if (_.isUndefined(attrs[validatedAttr]) && error) {
isValid = false;
break;
} else if(!error && view) {
options.valid(view, validatedAttr, options.selector);
}
if (error !== false && hasChildValidaton(validation, validatedAttr)) {
isValid = validateAll(model, validation[validatedAttr].validation, attrs[validatedAttr], computed);
Expand Down Expand Up @@ -113,7 +115,7 @@ Backbone.Validation = (function(Backbone, _, undefined) {
}

if (isValid) {
isValid = validateAll(model, validation, attrs, computed);
isValid = validateAll(model, validation, attrs, computed, view, options);
}

return {
Expand Down
81 changes: 81 additions & 0 deletions tests/general.js
Expand Up @@ -385,5 +385,86 @@ buster.testCase("Backbone.Validation", {

assert(this.model.isValid());
}
},

"when bound to model with to dependent attribute validations": {
setUp: function() {
var View = Backbone.View.extend({
render: function() {
var html = $('<input type="text" name="one" /><input type="text" name="two" />');
if(!this.$el) { // Backbone 0.5.3
this.$(this.el).append(html);
} else { // Backbone 0.9.0
this.$el.append(html);
}

Backbone.Validation.bind(this);
}
});
var Model = Backbone.Model.extend({
validation: {
one: function(val, attr, computed) {
if(val < computed.two) {
return 'error';
}
},
two: function(val, attr, computed) {
if(val > computed.one) {
return 'return';
}
}
}
});


this.model = new Model();
this.view = new View({
model: this.model
});

this.view.render();
this.one = $(this.view.$('[name~=one]'));
this.two = $(this.view.$('[name~=two]'));
},

tearDown: function() {
this.view.remove();
},

"when setting invalid value on second input": {
setUp: function() {
this.model.set({one:1});
this.model.set({two:2});
},

"first input is valid": function() {
if(Backbone.VERSION === '0.5.3'){
refute(this.one.hasClass('invalid'));
}
else {
assert(this.one.hasClass('invalid'));
}
},

"second input is invalid": function() {
assert(this.two.hasClass('invalid'));
}
},

"when setting invalid value on second input and changing first": {
setUp: function() {
this.model.set({one:1});
this.model.set({two:2});
this.model.set({one:2});
},

"first input is valid": function() {
refute(this.one.hasClass('invalid'));
},

"second input is valid": function() {
refute(this.two.hasClass('invalid'));
}
}
}
});

0 comments on commit 4589f26

Please sign in to comment.