Sha256: bca4ad60bce522a3c283776ecf8dee63ce0ca87c4164aa016254fdb0148ba0e1

Contents?: true

Size: 1017 Bytes

Versions: 3

Compression:

Stored size: 1017 Bytes

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]

    validate_nested!(options) # call valid? recursively and collect nested errors.

    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 validate_nested!(options)
    schema.each(twin: true) do |dfn|
      property_options = options.dup

      property_options[:prefix] = options[:prefix].dup # TODO: implement Options#dup.
      property_options[:prefix] << dfn.name

      # recursively call valid? on nested form.
      Disposable::Twin::PropertyProcessor.new(dfn, self).() { |form| form.validate!(property_options) }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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