Sha256: 81ddcb02e9a95e944d81d194bd613bf7e4d61520b6642bceda3bcf256aded5b8

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

module Reform::Contract::Validate
  module NestedValid
    def to_hash(*)
      nested_forms do |attr|
        attr.merge!(
          :serialize => lambda { |object, args|

            # FIXME: merge with Validate::Writer
            options = args.user_options.dup
            options[:prefix] = options[:prefix].dup # TODO: implement Options#dup.
            options[:prefix] << args.binding.name # FIXME: should be #as.

            object.validate!(options) # recursively call valid?
          },
        )
      end

      super
    end
  end

  def validate
    options = {:errors => errs = Reform::Contract::Errors.new(self), :prefix => []}

    validate!(options)

    self.errors = errs # if the AM valid? API wouldn't use a "global" variable this would be better.

    errors.valid?
  end
  def validate!(options)
    prefix = options[:prefix]

    # call valid? recursively and collect nested errors.
    mapper.new(fields).extend(NestedValid).to_hash(options) # TODO: only include nested forms here.

    valid?  # this validates on <Fields> using AM::Validations, currently.

    options[:errors].merge!(self.errors, prefix)
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reform-1.2.0.beta2 lib/reform/contract/validate.rb
reform-1.2.0.beta1 lib/reform/contract/validate.rb