Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Forked from nijikokun/m.validator.js
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save barneycarroll/09d0a3c4f3682310b260 to your computer and use it in GitHub Desktop.

Select an option

Save barneycarroll/09d0a3c4f3682310b260 to your computer and use it in GitHub Desktop.

Revisions

  1. barneycarroll revised this gist Mar 30, 2015. 1 changed file with 6 additions and 8 deletions.
    14 changes: 6 additions & 8 deletions m.validator.js
    Original file line number Diff line number Diff line change
    @@ -17,19 +17,17 @@ m.validator.prototype.clearErrors = function () {
    }

    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)
    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) {
    self.errors[key] = result
    this.errors[key] = result
    }
    })
    }

    return self
    }
  2. @nijikokun nijikokun revised this gist Mar 30, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion m.validator.js
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ m.validator.prototype.validate = function () {

    this.clearErrors()

    Object.keys(validations).forEach(function (key) {
    Object.keys(this.validations).forEach(function (key) {
    var validator = self.validations[key]
    var value = self.model[key]()
    var result = validator.bind(self.model)(value)
  3. @nijikokun nijikokun revised this gist Mar 30, 2015. 1 changed file with 30 additions and 32 deletions.
    62 changes: 30 additions & 32 deletions m.validator.js
    Original file line number Diff line number Diff line change
    @@ -2,36 +2,34 @@ m.validator = function (model, validations) {
    this.errors = {}
    this.validations = validations
    this.model = model

    this.hasErrors = function () {
    return Object.keys(this.errors).length
    }

    this.hasError = function (prop) {
    return this.errors[prop]
    }

    this.clearErrors = function () {
    this.errors = {}
    }

    this.validate = function () {
    var self = this

    this.clearErrors()

    Object.keys(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
    }

    return this
    }

    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(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
    }
  4. @nijikokun nijikokun revised this gist Mar 29, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion m.validator.js
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ m.validator = function (model, validations) {
    Object.keys(validations).forEach(function (key) {
    var validator = self.validations[key]
    var value = self.model[key]()
    var result = validator.bind(self.model, value)
    var result = validator.bind(self.model)(value)

    if (result) {
    self.errors[key] = result
  5. @nijikokun nijikokun revised this gist Mar 29, 2015. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions m.validator.js
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,25 @@
    m.validator = function (model, validations) {
    this.errors = {}
    this.validations = {}
    this.validations = validations
    this.model = model

    this.hasErrors = function () {
    return Object.keys(this.errors).length
    }

    this.clearErrors = function () {
    this.errors = {}
    }

    this.hasError = function (prop) {
    return this.errors[prop]
    }

    this.clearErrors = function () {
    this.errors = {}
    }

    this.validate = function () {
    var self = this

    this.clearErrors()

    Object.keys(validations).forEach(function (key) {
    var validator = self.validations[key]
    var value = self.model[key]()
  6. @nijikokun nijikokun created this gist Mar 29, 2015.
    35 changes: 35 additions & 0 deletions m.validator.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    m.validator = function (model, validations) {
    this.errors = {}
    this.validations = {}
    this.model = model

    this.hasErrors = function () {
    return Object.keys(this.errors).length
    }

    this.clearErrors = function () {
    this.errors = {}
    }

    this.hasError = function (prop) {
    return this.errors[prop]
    }

    this.validate = function () {
    var self = this

    Object.keys(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
    }

    return this
    }