Skip to content

Instantly share code, notes, and snippets.

@eyy
Last active December 23, 2015 03:19
Show Gist options
  • Select an option

  • Save eyy/6572557 to your computer and use it in GitHub Desktop.

Select an option

Save eyy/6572557 to your computer and use it in GitHub Desktop.
rivets jquery-validate binder (+ handle mongoose errors)
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