Skip to content
Browse files

Moved the format functions from the validators objects into its own a…

…nd mixes them back in when calling the validator
  • Loading branch information...
1 parent 1bb03b5 commit 27ed257795ba089a952d494268c3338db3cf1b82 @thedersen committed Nov 21, 2012
Showing with 27 additions and 22 deletions.
  1. +27 −17 src/backbone-validation.js
  2. +0 −5 tests/customValidators.js
View
44 src/backbone-validation.js
@@ -13,6 +13,27 @@ Backbone.Validation = (function(_){
};
+ // Helper functions used when formatting error messages
+ // ----------------------------------------------------
+
+ var formatFunctions = {
+ // Uses the configured label formatter to format the attribute name
+ // to make it more readable for the user
+ formatLabel: function(attrName, model) {
+ return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model);
+ },
+
+ // Replaces nummeric placeholders like {0} in a string with arguments
+ // passed to the function
+ format: function() {
+ var args = Array.prototype.slice.call(arguments);
+ var text = args.shift();
+ return text.replace(/\{(\d+)\}/g, function(match, number) {
+ return typeof args[number] !== 'undefined' ? args[number] : match;
+ });
+ }
+ };
+
// Validation
// ----------
@@ -70,7 +91,11 @@ Backbone.Validation = (function(_){
// applying all the validators and returning the first error
// message, if any.
return _.reduce(getValidators(model, attr), function(memo, validator){
- var result = validator.fn.call(defaultValidators, value, attr, validator.val, model, computed);
+ // Pass the format functions plus the default
+ // validators as the context to the validator
+ var ctx = _.extend({}, formatFunctions, defaultValidators),
+ result = validator.fn.call(ctx, value, attr, validator.val, model, computed);
+
if(result === false || memo === false) {
return false;
}
@@ -377,6 +402,7 @@ Backbone.Validation = (function(_){
}
};
+
// Built in validators
// -------------------
@@ -404,22 +430,6 @@ Backbone.Validation = (function(_){
};
return {
- // Uses the configured label formatter to format the attribute name
- // to make it more readable for the user
- formatLabel: function(attrName, model) {
- return defaultLabelFormatters[defaultOptions.labelFormatter](attrName, model);
- },
-
- // Replaces nummeric placeholders like {0} in a string with arguments
- // passed to the function
- format: function() {
- var args = Array.prototype.slice.call(arguments);
- var text = args.shift();
- return text.replace(/\{(\d+)\}/g, function(match, number) {
- return typeof args[number] !== 'undefined' ? args[number] : match;
- });
- },
-
// Function validator
// Lets you implement a custom function used for validation
fn: function(value, attr, fn, model, computed) {
View
5 tests/customValidators.js
@@ -31,11 +31,6 @@ buster.testCase('Extending Backbone.Validation with custom validator', {
refute(this.model.set({
age: 2
}));
- },
-
- "context is the validators object": function() {
- this.model.set({age:1});
- assert.same(Backbone.Validation.validators, this.context);
}
});

0 comments on commit 27ed257

Please sign in to comment.
Something went wrong with that request. Please try again.