Sha256: 1e150de1c393818c0133e17af0610589d828c846253b27cf44e08d4c34668733

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 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, 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).(@fields, errors, self)
  end
end

require "reform/validation/groups"

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reform-2.1.0 lib/reform/validation.rb
reform-2.1.0.rc1 lib/reform/validation.rb