Permalink
Browse files

BREAKING: The array with attribute names passed to the error events i…

…s replaced with an object with attribute name and error message {name: 'Name is required'}
  • Loading branch information...
1 parent d81b7d6 commit f85c6fea1e1d9ddd659d8552366609e54d8242ec @thedersen committed Jul 19, 2012
View
@@ -102,23 +102,20 @@
// as well as error messages.
var validateModel = function(model, attrs) {
var result, error, attr,
- invalidAttrs = [],
- errorMessages = [],
+ invalidAttrs = {},
isValid = true,
computed = _.clone(attrs);
for (attr in attrs) {
error = validateAttr(model, attr, attrs[attr], computed);
- if (error) {
- invalidAttrs.push(attr);
- errorMessages.push(error);
- isValid = false;
- }
+ if (error) {
+ invalidAttrs[attr] = error;
+ isValid = false;
+ }
}
return {
invalidAttrs: invalidAttrs,
- errorMessages: errorMessages,
isValid: isValid
};
};
@@ -170,11 +167,12 @@
// After validation is performed, loop through all changed attributes
// and call either the valid or invalid callback so the view is updated.
for(var attr in allAttrs) {
- var index = _.indexOf(result.invalidAttrs, attr);
- if(index !== -1 && (changedAttrs.hasOwnProperty(attr) || validateAll)){
- opt.invalid(view, attr, result.errorMessages[index], opt.selector);
+ var invalid = result.invalidAttrs.hasOwnProperty(attr),
+ changed = changedAttrs.hasOwnProperty(attr);
+ if(invalid && (changed || validateAll)){
+ opt.invalid(view, attr, result.invalidAttrs[attr], opt.selector);
}
- if(!_.include(result.invalidAttrs, attr)){
+ if(!invalid){
opt.valid(view, attr, opt.selector);
}
}
@@ -190,8 +188,8 @@
// Return any error messages to Backbone, unless the forceUpdate flag is set.
// Then we do not return anything and fools Backbone to believe the validation was
// a success. That way Backbone will update the model regardless.
- if (!opt.forceUpdate && _.intersection(result.invalidAttrs, _.keys(changedAttrs)).length > 0) {
- return result.errorMessages;
+ if (!opt.forceUpdate && _.intersection(_.keys(result.invalidAttrs), _.keys(changedAttrs)).length > 0) {
+ return result.invalidAttrs;
}
}
};
@@ -107,11 +107,11 @@ buster.testCase("Chaining built-in validators with custom", {
},
"violating first validator in chain return first error message": function() {
- assert.equals(['Name is required'], this.model.validate({name:''}));
+ assert.equals({name: 'Name is required'}, this.model.validate({name:''}));
},
"violating second validator in chain return second error message": function() {
- assert.equals(['error'], this.model.validate({name:'a'}));
+ assert.equals({name: 'error'}, this.model.validate({name:'a'}));
},
"violating none of the validators returns undefined": function() {
View
@@ -70,7 +70,7 @@ buster.testCase("Backbone.Validation events", {
this.model.bind('validated', function(valid, model, attr){
refute(valid);
assert.same(this.model, model);
- assert.equals(['age', 'name'], attr);
+ assert.equals({age:'age', name:'name'}, attr);
done();
}, this);
@@ -80,7 +80,7 @@ buster.testCase("Backbone.Validation events", {
"validated:invalid event is triggered with model and name of attributes with error": function(done) {
this.model.bind('validated:invalid', function(model, attr){
assert.same(this.model, model);
- assert.equals(['age', 'name'], attr);
+ assert.equals({age: 'age', name: 'name'}, attr);
done();
}, this);
@@ -90,7 +90,7 @@ buster.testCase("Backbone.Validation events", {
"error event is triggered with model and error as an array of attribute names with error": function(done) {
this.model.bind('error', function(model, error) {
assert.same(this.model, model);
- assert.equals(['age', 'name'], error);
+ assert.equals({age: 'age', name: 'name'}, error);
done();
}, this);
@@ -103,7 +103,7 @@ buster.testCase("Backbone.Validation events", {
this.model.bind('validated', function(valid, model, attrs){
refute(valid);
assert.same(this.model, model);
- assert.equals(['name'], attrs);
+ assert.equals({name: 'name'}, attrs);
done();
}, this);
@@ -115,7 +115,7 @@ buster.testCase("Backbone.Validation events", {
"validated:invalid event is triggered with model and an array with the names of the attributes with error": function(done) {
this.model.bind('validated:invalid', function(model, attrs){
assert.same(this.model, model);
- assert.equals(['name'], attrs);
+ assert.equals({name: 'name'}, attrs);
done();
}, this);
@@ -22,7 +22,7 @@ buster.testCase("acceptance validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Agree must be accepted'], error);
+ assert.equals({agree: 'Agree must be accepted'}, error);
done();
});
this.model.set({agree:false});
@@ -27,7 +27,7 @@ buster.testCase("equalTo validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Password repeat must be the same as Password'], error);
+ assert.equals({passwordRepeat: 'Password repeat must be the same as Password'}, error);
done();
});
this.model.set({passwordRepeat:'123'});
@@ -22,7 +22,7 @@ buster.testCase("length validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Postal code must be 2 characters'], error);
+ assert.equals({postalCode: 'Postal code must be 2 characters'}, error);
done();
});
this.model.set({postalCode:''});
View
@@ -22,7 +22,7 @@ buster.testCase("max validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Age must be less than or equal to 10'], error);
+ assert.equals({age: 'Age must be less than or equal to 10'}, error);
done();
});
this.model.set({age:11});
@@ -22,7 +22,7 @@ buster.testCase("maxLength validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Name must be at most 2 characters'], error);
+ assert.equals({name: 'Name must be at most 2 characters'}, error);
done();
});
this.model.set({name:'aaa'});
View
@@ -22,7 +22,7 @@ buster.testCase("min validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Age must be greater than or equal to 1'], error);
+ assert.equals({age: 'Age must be greater than or equal to 1'}, error);
done();
});
this.model.set({age: 0});
@@ -22,7 +22,7 @@ buster.testCase("minLength validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Name must be at least 2 characters'], error);
+ assert.equals({name: 'Name must be at least 2 characters'}, error);
done();
});
this.model.set({name:''});
@@ -22,7 +22,7 @@ buster.testCase("oneOf validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Country must be one of: Norway, Sweeden'], error);
+ assert.equals({country: 'Country must be one of: Norway, Sweeden' }, error);
done();
});
this.model.set({country:''});
@@ -28,7 +28,7 @@ buster.testCase("pattern validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Email must be a valid email'], error);
+ assert.equals({email: 'Email must be a valid email'}, error);
done();
});
this.model.set({email:''});
@@ -22,7 +22,7 @@ buster.testCase("range validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Age must be between 1 and 10'], error);
+ assert.equals({age: 'Age must be between 1 and 10'}, error);
done();
});
this.model.set({age:0});
@@ -22,7 +22,7 @@ buster.testCase("rangeLength validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Name must be between 2 and 4 characters'], error);
+ assert.equals({name: 'Name must be between 2 and 4 characters'}, error);
done();
});
this.model.set({name:'a'});
@@ -37,7 +37,7 @@ buster.testCase("required validator", {
"has default error message": function(done) {
this.model.bind('error', function(model, error){
- assert.equals(['Name is required'], error);
+ assert.equals({name: 'Name is required'}, error);
done();
});
this.model.set({name:''});

0 comments on commit f85c6fe

Please sign in to comment.