Just include the script file, and then instead of passing a function as the validate attribute, you pass an object literal:
var ValidatingModel = Backbone.Model.extend({
validate : {
name : {
required : true,
pattern : /^[a-zA-Z]+$/,
minlength : 3,
maxlength : 100
},
age : {
type: "number",
min: 0,
max: 200
},
email : {
type: "email"
},
homepage : {
type: "url"
},
occupation : {
in : [
"Lawyer",
"Doctor",
"Professor",
"Economist"
]
}
}
});
- in
- Takes an array of values, compares with ==
- pattern
- Takes a regex
- minlength
- maxlength
- min (number)
- max
- type
- url
- number
var ValidatingModel = Backbone.Model.extend({
validate: function(atts) {
// YUP!
}
});
var vm = new ValidatingModel;
vm.bind('invalid', function(model, errs) {
// wherein errors looks like this
// { name: ["required", "pattern", "minlength"] }
});
vm.bind('invalid:name', function(model, errs) {
// wherein errors looks like this:
// ["required", "pattern", "minlength"]
});
var CustomValidatingModel = Backbone.Model.extend({
validate: {
radicality: {
custom: "supercomplicatedlogic"
}
},
supercomplicatedlogic: function(attributeName, attributeValue) {
if (attributeValue != "awesome") {
return "supercomplicatedlogicfail";
}
}
});
Backbone.Validations.addValidator("awesomeValidator", function(options, attributeName, model, valueToSet) {
// do junk
// return something if you want to communicate stuff!
});
var CustomGlobalUsingValidationDudeModel = Backbone.Model.extend({
validate : {
name: {
awesomeValidator: "whatever"
}
}
});
Give 'er a go!
Backbone.Validations.noConflict();
// Now you can get at the validating model with this:
Backbone.Validations.Model
Copyright (C) 2011 Neal Stewart
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.