Sha256: be5313937eef48f2c3801d89a56a96262878b82788dcd89922e905c242fa0211

Contents?: true

Size: 1.04 KB

Versions: 6

Compression:

Stored size: 1.04 KB

Contents

# Adds ::validates and friends, and #valid? to the object.
# This is completely form-independent.
module Reform::Validation
  module ClassMethods
    def validation_groups
      @groups ||= Groups.new(validation_group_class) # TODO: inheritable_attr with Inheritable::Hash
    end

    # DSL.
    def validation(name=:default, options={}, &block)
      heritage.record(:validation, name, options, &block)

      group = validation_groups.add(name, options)

      group.instance_exec(&block)
    end

    def validates(*args, &block)
      validation(:default, inherit: true) { validates *args, &block }
    end

    def validate(*args, &block)
      validation(:default, inherit: true) { validate *args, &block }
    end

    def validates_with(*args, &block)
      validation(:default, inherit: true) { validates_with *args, &block }
    end
  end

  def self.included(includer)
    includer.extend(ClassMethods)
  end

  def valid?
    Groups::Result.new(self.class.validation_groups).(to_nested_hash, errors, self)
  end
end

require "reform/validation/groups"

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
reform-2.2.4 lib/reform/validation.rb
reform-2.2.3 lib/reform/validation.rb
reform-2.2.2 lib/reform/validation.rb
reform-2.2.1 lib/reform/validation.rb
reform-2.2.0 lib/reform/validation.rb
reform-2.2.0.rc1 lib/reform/validation.rb