Sha256: 0da3c0ef747e5f3bb91b6304a4422c15079a487570f875151b775eda5ea573fb

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

#AlphaConstraint = require "/hoarder/validator/constraints/alpha_constraint"
AlphanumericConstraint = require "hoarder/validator/constraints/alphanumeric_constraint"
CreditCardConstraint = require "hoarder/validator/constraints/credit_card_constraint"
EmailConstraint = require "hoarder/validator/constraints/email_constraint"
MaxLengthConstraint = require "hoarder/validator/constraints/max_length_constraint"
MinLengthConstraint = require "hoarder/validator/constraints/min_length_constraint"
NumericConstraint = require "hoarder/validator/constraints/numeric_constraint"
PhoneConstraint = require "hoarder/validator/constraints/phone_constraint"
RequiredConstraint = require "hoarder/validator/constraints/required_constraint"

#
# @author - Tim Shelburne <tim@musiconelive.com>
#
# a class to validate forms and form elements
#
class FormValidator
  constructor: (@constraints)->
    
  @default: ->
    new @([
      new AlphanumericConstraint()
      new CreditCardConstraint()
      new EmailConstraint()
      new MaxLengthConstraint()
      new MinLengthConstraint()
      new NumericConstraint()
      new PhoneConstraint()
      new RequiredConstraint()
    ])

  validateForm: (form)->
    errors = []
    for element in form.elements()
      errors = errors.concat @validateElement(element)
    errors

  validateElement: (element)->
    for rule in element.validationRules
      ruleParts = rule.split('=')
      type = ruleParts[0]
      context = {}
      context.value = ruleParts[1] if ruleParts.length > 1

      for constraint in @constraints
        if constraint.canHandle type
          errors = constraint.handle element, context
          if errors.length > 0
            element.addError(errors[0])
            return errors

    return []

return FormValidator

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hoarder-js-0.0.1 assets/scripts/coffee/hoarder/validator/form_validator.coffee