Permalink
Browse files

Required validator gets the same paramenters as method validator when…

… soecified as a function
  • Loading branch information...
1 parent 48ef8b1 commit e020dc8ac172e2c4cd35c99a53a2d23eb1b49f69 @thedersen committed Jul 19, 2012
Showing with 28 additions and 3 deletions.
  1. +2 −2 backbone.validation.js
  2. +26 −1 tests/validators/required.js
View
@@ -434,8 +434,8 @@
},
// Required validator
- required: function(value, attr, required, model) {
- var isRequired = _.isFunction(required) ? required.call(model) : required;
+ required: function(value, attr, required, model, computed) {
+ var isRequired = _.isFunction(required) ? required.call(model, value, attr, computed) : required;
if(!isRequired && !hasValue(value)) {
return false; // overrides all other validators
}
@@ -10,7 +10,10 @@ buster.testCase("required validator", {
required: true
},
dependsOnName: {
- required: function() {
+ required: function(val, attr, computed) {
+ that.ctx = this;
+ that.attr = attr;
+ that.computed = computed;
return this.get('name') === 'name';
}
}
@@ -94,5 +97,27 @@ buster.testCase("required validator", {
refute(this.model.set({
dependsOnName: undefined
}));
+ },
+
+ "context is the model": function() {
+ this.model.set({
+ dependsOnName: ''
+ });
+ assert.same(this.ctx, this.model);
+ },
+
+ "second argument is the name of the attribute being validated": function() {
+ this.model.set({dependsOnName: ''});
+ assert.equals('dependsOnName', this.attr);
+ },
+
+ "third argument is a computed model state": function() {
+ this.model.set({attr: 'attr'}, {silent: true});
+ this.model.set({
+ name: 'name',
+ dependsOnName: 'value'
+ });
+
+ assert.equals({agree:true, attr:'attr', dependsOnName:'value', name:'name'}, this.computed);
}
});

0 comments on commit e020dc8

Please sign in to comment.