Skip to content

Instantly share code, notes, and snippets.

@sgreenfield
Created November 23, 2012 23:03
Show Gist options
  • Save sgreenfield/4137653 to your computer and use it in GitHub Desktop.
Save sgreenfield/4137653 to your computer and use it in GitHub Desktop.
//meta validation
//idea for dynamic validation to enable model validation to change its own validation on the fly
//example syntax below
var validation = {
rules: [ //model attribute rules
'recipient_type': {
//on: true, //always on by default
when: [
{ blank: true, trigger: 'fail' },
{
val: 'SMS',
trigger: function(rules, self){ //rules = the rules model with extra functions attached, self = the 'recipient_type' rules block
//this will only happen if the value is SMS, if the value changes to anything else, the rules will revert back to the originals
rules.turn_off('user_id');
rules.turn_on('sms_number');
},
//revert: true //true by default
}
]
},
'user_id': {
when: [
{ blank: true, trigger: 'fail' },
{
fail: true, //if it fails
trigger: function(rules, self){
//this will only happen if user_id fails
self.message = 'Please select a user';
}
}
]
},
'sms_number': {
on: false, //this rule is off unless explicitly turned on
when: [
{ blank: true, trigger: 'fail' },
{
fail: true, //if it fails
trigger: function(rules, self){
//this will only happen if user_id fails
self.message = 'Please enter a SMS number';
}
}
]
}
]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment