Skip to content

Commit 710b1af

Browse files
author
Luiz Américo
committed
Pre validate multiple attributes thedersen#303
1 parent a256ef6 commit 710b1af

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

src/backbone-validation.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,26 @@ Backbone.Validation = (function(_){
206206
preValidate: function(attr, value) {
207207
var self = this,
208208
result = {},
209-
error;
209+
error,
210+
allAttrs = _.extend({}, this.attributes);
210211

211212
if(_.isObject(attr)){
212-
_.each(attr, function(value, key) {
213-
error = self.preValidate(key, value);
213+
// if multiple attributes are passed at once we would like for the validation functions to
214+
// have access to the fresh values sent for all attributes, in the same way they do in the
215+
// regular validation
216+
_.extend(allAttrs, attr);
217+
218+
_.each(attr, _.bind(function(value, attrKey) {
219+
error = validateAttr(this, attrKey, value, allAttrs);
214220
if(error){
215-
result[key] = error;
221+
result[attrKey] = error;
216222
}
217-
});
223+
}, this));
218224

219225
return _.isEmpty(result) ? undefined : result;
220226
}
221227
else {
222-
return validateAttr(this, attr, value, _.extend({}, this.attributes));
228+
return validateAttr(this, attr, value, allAttrs);
223229
}
224230
},
225231

test/preValidate.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,50 @@ module.exports = {
6868
refute(this.model.preValidate({ name: 'name' }));
6969
}
7070
}
71-
}
72-
}
71+
},
72+
73+
"when model has dependancies between validation functions": {
74+
setUp: function() {
75+
var CARD_TYPES = {
76+
VISA : 0,
77+
AMEX : 1
78+
};
79+
var Model = Backbone.Model.extend({
80+
validation: {
81+
card_type: {
82+
required: true
83+
},
84+
security_code: function(value, attr, computedState){
85+
var requiredLength = (computedState.card_type === CARD_TYPES.AMEX? 4 : 3);
86+
if(value && _.isString(value) && value.length !== requiredLength) {
87+
return 'Please enter a valid security code.';
88+
}
89+
}
90+
}
91+
});
92+
Model.CARD_TYPES = CARD_TYPES;
93+
this.ModelDefinition = Model;
94+
this.model = new Model();
95+
Backbone.Validation.bind(new Backbone.View({model: this.model}));
96+
},
97+
98+
"and pre-validating hash of attributes": {
99+
"returns error object when value is not valid": function() {
100+
var result = this.model.preValidate({card_type: this.ModelDefinition.CARD_TYPES.VISA, security_code: '1234'});
101+
assert(result.security_code);
102+
refute(result.card_type);
103+
},
73104

105+
"returns error object when values are not valid": function() {
106+
var result = this.model.preValidate({card_type: '', security_code: '12345'});
107+
assert(result.card_type);
108+
assert(result.security_code);
109+
},
110+
111+
"returns nothing when value is valid": function() {
112+
refute(this.model.preValidate({card_type: this.ModelDefinition.CARD_TYPES.AMEX, security_code: '1234'}));
113+
}
114+
}
115+
}
116+
}
74117
}

0 commit comments

Comments
 (0)