Module: Reform::Form::ActiveModel::Validations

Included in:
Validator
Defined in:
lib/reform/form/active_model/validations.rb

Overview

AM::Validations for your form.

Note: The preferred way for validations should be Lotus::Validations, as ActiveModel::Validation’s implementation is old, very complex given that it needs to do a simple thing, and it’s using globals like @errors.

Implements ::validates and friends, and #valid?.

Defined Under Namespace

Classes: Validator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/reform/form/active_model/validations.rb', line 13

def self.included(includer)
  includer.instance_eval do
    extend Uber::InheritableAttr
    inheritable_attr :validator
    self.validator = Class.new(Validator)

    class << self
      extend Uber::Delegates
      delegates :validator, :validates, :validate, :validates_with, :validate_with
    end
  end
end

Instance Method Details

#build_errorsObject



26
27
28
# File 'lib/reform/form/active_model/validations.rb', line 26

def build_errors
  Reform::Contract::Errors.new(self)
end

#valid?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/reform/form/active_model/validations.rb', line 61

def valid?
  validator = self.class.validator.new(self)
  validator.valid? # run the Validations object's validator with the form as context. this won't pollute anything in the form.


  #errors.merge!(validator.errors, "")
  validator.errors.each do |name, error| # TODO: handle with proper merge, or something. validator.errors is ALWAYS AM::Errors.
    errors.add(name, error)
  end

  errors.empty?
end