Sha256: 39124cff9a8a921e0d10fc02a5ed827f6974cb37a657598a672dc89e98aa66d3

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

module Reform::Contract::Validate
  module NestedValid
    def to_hash(*)
      nested_forms do |attr|
        # attr.delete(:prepare)
        # attr.delete(:extend)

        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.

            # puts "======= user_options: #{args.user_options.inspect}"

            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)
    # puts "validate! in #{self.class.name}: #{true.inspect}"
    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

4 entries across 4 versions & 1 rubygems

Version Path
reform-1.1.1 lib/reform/contract/validate.rb
reform-1.1.0 lib/reform/contract/validate.rb
reform-1.0.4 lib/reform/contract/validate.rb
reform-1.0.3 lib/reform/contract/validate.rb