Last active
December 19, 2015 00:29
-
-
Save rex/5869213 to your computer and use it in GitHub Desktop.
Validation plugin, using jQuery for plugin and Underscore mixins for functionality.
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
// In Router.js, whatever route needs validation | |
Router({ | |
//Blahblahblah | |
somePage : function(e) { | |
// Build all the things! | |
var SomeModule = zngine.module('someModule') | |
var ThisView = new SomeModule.SomeView( someParams ) | |
ThisView.render() | |
} | |
}) | |
// In SomeModule.js | |
SomeModule.SomeView = Backbone.View.extend({ | |
el : 'blah', | |
events : { | |
'click #thisIsTheButtonToEnableWhenValidationPasses' : 'doForm' | |
}, | |
validatorConfig : { | |
submitButton : '#thisIsTheButtonToEnableWhenValidationPasses', | |
fields : { | |
'fieldName' : { | |
selector : '#FieldSelector', | |
msg : 'This is the error message to display beneath the input on failed validation.', | |
validators : { | |
isString : true, // Explicit Underscore method names | |
size : { | |
min : 4, | |
max : 10 | |
}, | |
noNumbers : true | |
} | |
} | |
} | |
}, | |
initialize : function() {}, | |
render : function() { | |
// Build all the things! | |
this.$el.validator( this.ValidatorConfig ) | |
}, | |
doForm : function(e) { | |
// Process form normally | |
} | |
}) | |
// jQuery Plugin | |
$(function() { | |
$.validator({ | |
submitButton : '#thisIsTheButtonToEnableWhenValidationPasses', | |
'fieldName' : { | |
selector : '#FieldSelector', | |
msg : 'This is the error message to display beneath the input on failed validation.', | |
validators : { | |
isString : true, // Explicit Underscore method names | |
size : { | |
min : 4, | |
max : 10 | |
}, | |
noNumbers : true | |
} | |
} | |
}, model, context) | |
}) |
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
// In Router.js, whatever route needs validation | |
View({ | |
events : { | |
'click #submit' : 'someFunction', | |
'click #another' : 'anotherFunction' | |
}, | |
//Blahblahblah | |
validate : function(success, error, context) { | |
_.validate( this.validatorConfig, this.model.toJSON(), success, error, context ) | |
}, | |
someFunction : function(e) { | |
e.preventDefault() | |
this.validate(function() { | |
// Success | |
}, function() { | |
// Failure | |
}, this) | |
} | |
}) | |
// jQuery Plugin | |
_.mixin({ | |
validator : function(config, model, context) { | |
// Stuff here | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment