Skip to content

Instantly share code, notes, and snippets.

@m-vdb
Created December 11, 2012 12:58
Show Gist options
  • Save m-vdb/4258363 to your computer and use it in GitHub Desktop.
Save m-vdb/4258363 to your computer and use it in GitHub Desktop.
Backbone forms validator
(function(){
var validators = Backbone.Form.validators;
//a validator verifying if an element is not in a given list
validators.errMessages.notInList = 'Must not be in list {{list}}';
validators.notInList = function(options) {
if (!options.list) throw new Error('Missing required "list" options for "notInList" validator');
options = _.extend({
type: 'notInList',
message: this.errMessages.notInList
}, options);
return function notInList(value) {
options.value = value;
var err = {
type: options.type,
message: Form.helpers.createTemplate(options.message, options)
};
if (value === null || value === undefined || value === '') return;
if(_.indexOf(options.list, value) != -1) return err;
};
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment