Skip to content

Instantly share code, notes, and snippets.

@thedersen
Created July 27, 2012 19:11
Show Gist options
  • Save thedersen/3189894 to your computer and use it in GitHub Desktop.
Save thedersen/3189894 to your computer and use it in GitHub Desktop.
Validation dependent on other attribute
var Model = Backbone.Model.extend({
validation: {
dependsOn: function(value, attributeName, computedState) {
// computedState is the model's state after the current operation completes.
// You need to get the value of the other attribute from this, and not from the
// model it self because if both attributes are being set at the same time,
// model's value is not yet updated.
if(computedState.someAttribute === 'someValue') {
// If some condition is true, execute the min validator, else don't do anything.
return Backbone.Validation.validators.min(value, attributeName, 5 /*the min value*/, this);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment