named method validator

thedersen edited this page Jan 21, 2012 · 3 revisions
var SomeModel = Backbone.Model.extend({
  validation: {
    name: 'validateName'
  },
  validateName: function(value, attr) {
	if(value !== 'something') {
      return 'Name is invalid';
    }
  }
});

var SomeModel = Backbone.Model.extend({
  validation: {
    name: {
	  fn: 'validateName'
	}
  },
  validateName: function(value, attr) {
	if(value !== 'something') {
      return 'Name is invalid';
    }
  }
});
  • is invalid when method returns error message
  • is valid when method returns undefined
  • context is the model
  • second argument is the name of the attribute being validated