Skip to content

Commit 3861c76

Browse files
committed
Fixed undefined format function when calling one of the built in
validators form within a method validator. Fixes #98 and #111
1 parent 2442aa3 commit 3861c76

7 files changed

+39
-2
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,10 @@ Basic behaviour:
819819
820820
## Release notes
821821
822+
#### Master
823+
824+
* Fixed undefined format function when calling one of the built in validators form within a method validator. Fixes #98
825+
822826
#### v0.8.2 [commits](https://github.com/thedersen/backbone.validation/compare/v0.8.1...v0.8.2)
823827
824828
* `preValidate` now accepts a hash of attributes in addition to a key/value

dist/backbone-validation-amd-min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/backbone-validation-amd.js

+4
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,10 @@
626626
};
627627
}());
628628

629+
_.each(defaultValidators, function(validator, key){
630+
defaultValidators[key] = _.bind(defaultValidators[key], _.extend({}, formatFunctions, defaultValidators));
631+
});
632+
629633
return Validation;
630634
}(_));
631635
return Backbone.Validation;

dist/backbone-validation-min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/backbone-validation.js

+4
Original file line numberDiff line numberDiff line change
@@ -619,5 +619,9 @@ Backbone.Validation = (function(_){
619619
};
620620
}());
621621

622+
_.each(defaultValidators, function(validator, key){
623+
defaultValidators[key] = _.bind(defaultValidators[key], _.extend({}, formatFunctions, defaultValidators));
624+
});
625+
622626
return Validation;
623627
}(_));

src/backbone-validation.js

+6
Original file line numberDiff line numberDiff line change
@@ -612,5 +612,11 @@ Backbone.Validation = (function(_){
612612
};
613613
}());
614614

615+
// Set the correct context for all validators
616+
// when used form within a method validator
617+
_.each(defaultValidators, function(validator, key){
618+
defaultValidators[key] = _.bind(defaultValidators[key], _.extend({}, formatFunctions, defaultValidators));
619+
});
620+
615621
return Validation;
616622
}(_));

tests/validators/method.js

+19
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,23 @@ buster.testCase("method validator short hand syntax", {
120120

121121
assert.equals({attr:'attr', name:'name', age:1}, this.computed);
122122
}
123+
});
124+
125+
buster.testCase("method validator using other built in validator(s)", {
126+
setUp: function() {
127+
var Model = Backbone.Model.extend({
128+
validation: {
129+
name: function(val, attr, computed) {
130+
return Backbone.Validation.validators.length(val, attr, 4, this);
131+
}
132+
}
133+
});
134+
135+
_.extend(Model.prototype, Backbone.Validation.mixin);
136+
this.model = new Model();
137+
},
138+
139+
"it should format the error message returned from the built in validator": function(){
140+
assert.equals('Name must be 4 characters', this.model.preValidate('name', ''));
141+
}
123142
});

0 commit comments

Comments
 (0)