Created
January 15, 2014 19:19
-
-
Save jquense/8442540 to your computer and use it in GitHub Desktop.
the horror of trying to validate models rather than inputs
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
function validate(models, form) { | |
var validator = form.kendoValidator().getKendoValidator(); | |
_.extend(validator.options.rules, getValidationShims(validator, models, form, validationError)) | |
return validator.validate() | |
} | |
function validationError(text, models){ | |
return text + '\n' + _.pluck(models, 'ServiceType').join('\n') | |
} | |
function getValidationShims(validator, models, form, msgFn) { | |
// LOOK AT THIS KENDO | |
function msg(key) { | |
return function (errors) { | |
validator.options.messages[key] = function (input) { | |
msgFn( input.attr( "data-val-" + key ), errors) | |
} | |
}; | |
} | |
return _.mapValues(remoteShim, function (val, key) { | |
return _.partialRight(remoteShim[key], models, msg( key ) ) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment