Permalink
Browse files

Can pass the name of an attribute or an array of names to to verify i…

…f the attribute(s) are valid
  • Loading branch information...
1 parent d40086f commit c73d502d4f120f9640286718a2217cfc04dc9852 @thedersen committed Feb 20, 2012
Showing with 86 additions and 3 deletions.
  1. +17 −1 README.md
  2. +13 −2 backbone.validation.js
  3. +56 −0 tests/isValid.js
View
@@ -154,7 +154,22 @@ All validated models has a method that is used to force an validation to occur:
All validated models has a method that is used to check for the model's valid state: `model.isValid()`.
`isValid` returns `undefined` when no validation has occurred and the model has validation, otherwise, `true` or `false`.
-If you pass `true` as an argument, this will force an validation before the result is returned.
+
+If you pass `true` as an argument, this will force an validation before the result is returned:
+
+```js
+model.isValid(true);
+```
+
+If you pass the name of an attribute or an array of names, you can check whether or not the attributes are valid:
+
+```js
+// Check if name is valid
+model.isValid('name');
+
+// Check if name and age are valid
+model.isValid(['name', 'age']);
+```
### What gets validated when?
@@ -596,6 +611,7 @@ The message can contain placeholders for arguments that will be replaced:
#### v0.5.1
* `error` argument passed to the error event raised by Backbone is always an array
+* Can pass the name of an attribute or an array of names to `isValid` to verify if the attribute(s) are valid
#### v0.5.0
View
@@ -125,8 +125,19 @@ Backbone.Validation = (function(Backbone, _, undefined) {
var mixin = function(view, options) {
return {
- isValid: function(forceValidation) {
- if(forceValidation) {
+ isValid: function(option) {
+ if(_.isString(option)){
+ return !validateAttr(this, this.validation, option, this.get(option), this.toJSON());
+ }
+ if(_.isArray(option)){
+ for (var i = 0; i < option.length; i++) {
+ if(validateAttr(this, this.validation, option[i], this.get(option[i]), this.toJSON())){
+ return false;
+ }
+ }
+ return true;
+ }
+ if(option === true) {
this.validate();
}
return this.validation ? this._isValid : true;
View
@@ -41,7 +41,63 @@ buster.testCase("isValid", {
},
"can force validation by passing true": function() {
+ refute.defined(this.model.isValid());
assert(this.model.isValid(true) === false);
+ },
+
+ "and passing name of attribute": {
+ setUp: function() {
+ this.model.validation = {
+ name: {
+ required: true
+ },
+ age: {
+ required: true
+ }
+ };
+ },
+
+ "returns false when attribute is invalid": function() {
+ refute(this.model.isValid('name'));
+ },
+
+ "returns true when attribute is valid": function() {
+ this.model.set({name: 'name'}, { silent:true });
+
+ assert(this.model.isValid('name'));
+ }
+ },
+
+ "and passing array of attributes": {
+ setUp: function() {
+ this.model.validation = {
+ name: {
+ required: true
+ },
+ age: {
+ required: true
+ },
+ phone: {
+ required: true
+ }
+ };
+ },
+
+ "returns false when all attributes are invalid": function() {
+ refute(this.model.isValid(['name', 'age']));
+ },
+
+ "returns false when one attribute is invalid": function() {
+ this.model.set({name: 'name'}, { silent:true });
+
+ refute(this.model.isValid(['name', 'age']));
+ },
+
+ "returns true when all attributes are valid": function() {
+ this.model.set({name: 'name', age: 1 }, { silent:true });
+
+ assert(this.model.isValid(['name', 'age']));
+ }
}
}
});

0 comments on commit c73d502

Please sign in to comment.