diff --git a/src/backbone-validation.js b/src/backbone-validation.js index f9042507..1482019c 100644 --- a/src/backbone-validation.js +++ b/src/backbone-validation.js @@ -159,12 +159,12 @@ Backbone.Validation = (function(_){ // Loops through the model's attributes and validates them all. // Returns and object containing names of invalid attributes // as well as error messages. - var validateModel = function(model, attrs) { + var validateModel = function(model, attrs, validatedKeys) { var error, invalidAttrs = {}, isValid = true, computed = _.clone(attrs), - flattened = flatten(attrs); + flattened = _.pick(flatten(attrs), validatedKeys); _.each(flattened, function(val, attr) { error = validateAttr(model, attr, val, computed); @@ -239,7 +239,7 @@ Backbone.Validation = (function(_){ allAttrs = _.extend({}, validatedAttrs, model.attributes, attrs), changedAttrs = flatten(attrs || allAttrs), - result = validateModel(model, allAttrs); + result = validateModel(model, allAttrs, _.keys(validatedAttrs)); model._isValid = result.isValid; diff --git a/tests/attributesOption.js b/tests/attributesOption.js index 6029c063..c9efe0e5 100644 --- a/tests/attributesOption.js +++ b/tests/attributesOption.js @@ -17,6 +17,9 @@ buster.testCase("Setting options.attributes", { }, password: { required: true + }, + email: { + pattern: 'email' } } }); @@ -51,13 +54,15 @@ buster.testCase("Setting options.attributes", { assert.defined(errors.name); assert.defined(errors.age); refute.defined(errors.password); + refute.defined(errors.email); }, "when all the attributes in array are valid": { setUp: function () { this.model.set({ age: 1, - name: 'hello' + name: 'hello', + email: 'invalidemail' }); }, "validation will pass": function () { @@ -84,13 +89,15 @@ buster.testCase("Setting options.attributes", { assert.defined(errors.name); assert.defined(errors.age); refute.defined(errors.password); + refute.defined(errors.email); }, "when all the attributes returned by the function are valid": { setUp: function () { this.model.set({ age: 1, - name: 'hello' + name: 'hello', + email: 'invalidemail' }); }, "validation will pass": function () { @@ -115,6 +122,7 @@ buster.testCase("Setting options.attributes", { assert.defined(errors.name); refute.defined(errors.age); refute.defined(errors.password); + refute.defined(errors.email); }, "submit and buttons should not be included in attribute array": function () { @@ -127,7 +135,8 @@ buster.testCase("Setting options.attributes", { "when all the attributes present in form are valid": { setUp: function () { this.model.set({ - name: 'hello' + name: 'hello', + email: 'invalidemail' }); }, "validation will pass": function () { @@ -159,10 +168,11 @@ buster.testCase("Setting options.attributes", { refute.defined(errors.password); }, - "when all the attributes returned by registered the function are valid": { + "when all the attributes returned by the registered function are valid": { setUp: function () { this.model.set({ - age: 1 + age: 1, + email: 'invalidemail' }); }, "validation will pass": function () {