Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mithril validation utility
m.validator = function (model, validations) {
this.errors = {}
this.validations = validations
this.model = model
}
m.validator.prototype.hasErrors = function () {
return Object.keys(this.errors).length
}
m.validator.prototype.hasError = function (prop) {
return this.errors[prop]
}
m.validator.prototype.clearErrors = function () {
this.errors = {}
}
m.validator.prototype.validate = function () {
var self = this
this.clearErrors()
Object.keys(this.validations).forEach(function (key) {
var validator = self.validations[key]
var value = self.model[key]()
var result = validator.bind(self.model)(value)
if (result) {
self.errors[key] = result
}
})
return self
}
@iamjohnlong

This comment has been minimized.

Copy link

commented Oct 8, 2015

Can you show an example of how you should use this? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.