Update: If you are looking for documentation for Backbone.Validation it can be found here

I just released version 0.2.0 of my validation plugin for Backbone.js. The plugin lets you declare validation rules on your Backbone model, ensuring that input from the user is valid. It currently ships with 18 built-in validators such as required, min, max, range, length, minLength, maxLength, regex-validator etc. In addition to the built-in validators, it is very easy to extend with your custom validators.

Lets start with an example of how to declare validation rules on your model:

 1 var SomeModel = Backbone.Model.extend({
 2   validation: {
 3     name: {
 4       required: true,
 5       msg: 'Name is required'
 6     },
 7     age: {
 8       range: [1, 80]
 9     },
10     email: {
11       pattern: 'email'
12     },
13     someAttribute: function(value) {
14       if(value !== 'somevalue') {
15         return 'Error';
16       }
17     }
18   }
19 });

To configure your validation rules, simply add a validation object with a property for each attribute you want to validate on your model. The validation rules can either be an object with one of the built-in validators or a combination of two or more of them, or a function where you implement your own custom validation logic. If you want to provide a custom error message when using one of the built-in validators, simply define the msg property with your message.

To add your own validators, extend the Backbone.Validation.validators object as shown in the example below where we use underscore.js extend method. You can also redefine one or more of the built-in validators if it doesn't fit your needs.

 1 _.extend(Backbone.Validation.validators, {
 2   myValidator: function(value, attr, customValue, model) {
 3     if(value !== customValue) {
 4       return 'error';
 5     }
 6   },
 7   required: function(value, attr, customValue, model) {
 8     if(!value) {
 9       return 'My version of the required validator';
10     }
11   },
12 });
14 var Model = Backbone.Model.extend({
15   validation: {
16     age: {
17       myValidator: 1 // uses your custom validator
18     }
19   }
20 });

A couple conventions

After performing validation, an isValid attribute is set on the model that is (obviously) true when all the attributes on the model is valid, otherwise it is false.

The Backbone.Validation.callbacks contains two methods: valid and invalid. These are called (in addition to the error event raised by Backbone) after validation of an attribute is performed.

The default implementation of invalid tries to look up an element within the view with an id equal to the name of the attribute that is validated. If it finds one, an invalid class is added to the element as well as a data-error attribute with the error message. The valid method removes these if they exists. You can also configure it to use a class selector instead of id selector.

As with the validators, you can redefined the behavior of these callbacks by overriding the implementation.

1 _.extend(Backbone.Validation.callbacks, {
2   valid: function(view, attr, selector) {
3     // do something
4   },
5   invalid: function(view, attr, error, selector) {
6     // do something
7   }
8 });

Validation binding

For all of this to work, you need to hook up the validation. The validation binding code is executed with a call to Backbone.Validation.bind(view). The validate method on the view's model is then overridden to perform the validation.

There are several places that it can be called from, depending on your circumstances.

 1 // Binding when rendering
 2 var SomeView = Backbone.View.extend({
 3   render: function(){
 4     Backbone.Validation.bind(this);
 5   }
 6 });
 8 // Binding when initializing
 9 var SomeView = Backbone.View.extend({
10   initialize: function(){
11     Backbone.Validation.bind(this);
12   }
13 });
15 // Binding from outside a view
16 var SomeView = Backbone.View.extend({
17 });
18 var someView = new SomeView();
19 Backbone.Validation.bind(someView);


With this declarative way of defining validation, combined with the conventional updating of your user interface, it makes for an easy way to handle client validation. At least I think so.

But remember: you add client validation to be nice to your users, but you also need to add server validation to be nice to yourself and your company.

The source code for this project is licensed under the MIT License and is available on GitHub.