Sha256: 2277939af3d7a8363c433cef82944e4e9689081abf7c3e4868a30875b5fb90be

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

module Reform::Contract::Validate
  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.
    valid_representer.new(fields).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

private

  # runs form.validate! on all nested forms
  def valid_representer
    self.class.representer(:valid) do |dfn|
      dfn.merge!(
        :serialize => lambda { |form, args|
          options = args.user_options.dup
          options[:prefix] = options[:prefix].dup # TODO: implement Options#dup.
          options[:prefix] << args.binding.name # FIXME: should be #as.

          form.validate!(options) # recursively call valid? on nested form.
        }
      )
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
reform-1.2.6 lib/reform/contract/validate.rb
reform-1.2.5 lib/reform/contract/validate.rb
reform-1.2.4 lib/reform/contract/validate.rb
reform-1.2.3 lib/reform/contract/validate.rb
reform-1.2.2 lib/reform/contract/validate.rb
reform-1.2.1 lib/reform/contract/validate.rb