Last active
December 23, 2015 03:19
-
-
Save eyy/6572557 to your computer and use it in GitHub Desktop.
rivets jquery-validate binder (+ handle mongoose errors)
This file contains hidden or 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
| rivets.binders['on-valid'] = { | |
| 'function': true, | |
| unbind: function(el) { | |
| $(el).data('validator', null) | |
| .unbind('validate'); | |
| }, | |
| routine: function(el, value) { | |
| var handler = this.handler = this.eventHandler(value); | |
| return $(el) | |
| .on('submit', function() { return false; }) | |
| .validate({ | |
| submitHandler: function(form) { | |
| var validator = this, | |
| promise = handler({ | |
| target: $(form), | |
| validator: validator, | |
| preventDefault: $.noop | |
| }); | |
| if (!promise.fail) | |
| return; | |
| // handle mongoose errors | |
| promise.fail(function(res) { | |
| res = res.responseJSON; | |
| if (!res) | |
| return console.error('ERROR', promise.responseText); | |
| if (!res.errors) | |
| return; | |
| var errors = {}; | |
| $.each(res.errors, function(k, v) { | |
| errors[k] = v.message; | |
| }); | |
| validator.showErrors(errors); | |
| }); | |
| } | |
| }); | |
| } | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment