Created
July 27, 2012 19:11
-
-
Save thedersen/3189894 to your computer and use it in GitHub Desktop.
Validation dependent on other attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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