-
-
Save barneycarroll/09d0a3c4f3682310b260 to your computer and use it in GitHub Desktop.
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
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 () { | |
this.clearErrors() | |
for( var key in this.validations ){ | |
var validator = this.validations[key] | |
var value = this.model[key]() | |
var result = validator.bind(this.model)(value) | |
if (result) { | |
this.errors[key] = result | |
} | |
} | |
return self | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment