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, is using globals like @errors, and relies and more than 100 methods to be mixed into your form.

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/reform/form/active_model/validations.rb', line 15

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

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

      # Hooray! Delegate translation back to Reform's Validator class which contains AM::Validations.
      delegates :validator, :human_attribute_name, :lookup_ancestors, :i18n_scope # Rails 3.1.
    end
  end
end

Instance Method Details

#build_errorsObject



32
33
34
# File 'lib/reform/form/active_model/validations.rb', line 32

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

#read_attribute_for_validation(name) ⇒ Object

The concept of “composition” has still not arrived in Rails core and they rely on 400 methods being available in one object. This is why we need to provide parts of the I18N API in the form.



38
39
40
# File 'lib/reform/form/active_model/validations.rb', line 38

def read_attribute_for_validation(name)
  send(name)
end