Class: Form::ActiveModel::Validations::Result::ResultErrors

Inherits:
Reform::Contract::Result::Errors
  • Object
show all
Defined in:
lib/reform/form/active_model/validations.rb

Overview

to expose via #errors. i hate it.

Instance Method Summary collapse

Constructor Details

#initialize(a, b, success, amv_errors) ⇒ ResultErrors

Returns a new instance of ResultErrors.



125
126
127
128
129
# File 'lib/reform/form/active_model/validations.rb', line 125

def initialize(a, b, success, amv_errors)
  super(a, b)
  @success = success
  @amv_errors = amv_errors
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



161
162
163
# File 'lib/reform/form/active_model/validations.rb', line 161

def method_missing(m, *args, &block)
  @amv_errors.send(m, *args, &block) # send all methods to the AMV errors, even privates.
end

Instance Method Details

#[](k) ⇒ Object



135
136
137
# File 'lib/reform/form/active_model/validations.rb', line 135

def [](k)
  super(k.to_sym) || []
end

#add(key, error_text) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/reform/form/active_model/validations.rb', line 144

def add(key, error_text)
  # use rails magic to get the correct error_text and make sure we still update details and fields
  error = @amv_errors.add(key, error_text)
  error = [error.message] unless error.is_a?(Array)

  # using error_text instead of text to either keep the symbol which will be
  # magically replaced with the translate or directly the string - this is also
  # required otherwise in the custom_errors method we will add the actual message in the
  # ActiveModel::Errors#details which is not correct if a symbol was passed here
  Reform::Contract::CustomError.new(key, error_text, @result.to_results)

  # but since messages method is actually already defined in `Reform::Contract::Result::Errors
  # we need to update the @dotted_errors instance variable to add or merge a new error
  @dotted_errors.key?(key) ? @dotted_errors[key] |= error : @dotted_errors[key] = error
  instance_variable_set(:@dotted_errors, @dotted_errors)
end

#empty?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/reform/form/active_model/validations.rb', line 131

def empty?
  @success
end

#full_messagesObject



169
170
171
172
173
174
175
# File 'lib/reform/form/active_model/validations.rb', line 169

def full_messages
  base_errors = @amv_errors.full_messages
  form_fields = @amv_errors.instance_variable_get(:@base).instance_variable_get(:@fields)
  nested_errors = full_messages_for_nested_fields(form_fields)
  
  [base_errors, nested_errors].flatten.compact
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/reform/form/active_model/validations.rb', line 165

def respond_to?(method)
  @amv_errors.respond_to?(method) ? true : super
end

#to_sObject

rails expects this to return a stringified hash of the messages



140
141
142
# File 'lib/reform/form/active_model/validations.rb', line 140

def to_s
  messages.to_s
end