Skip to content

Commit

Permalink
Convert error message to a proper format when adding
Browse files Browse the repository at this point in the history
Now `ActiveModel::Errors#add` returns an instance of
`ActiveModel::Error`. So can't use it as is.
  • Loading branch information
y-yagi committed Feb 10, 2021
1 parent 250b4b0 commit 4762348
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/reform/form/active_model/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ def to_s

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

# 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
Expand All @@ -153,7 +154,7 @@ def add(key, error_text)

# 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] |= text : @dotted_errors[key] = text
@dotted_errors.key?(key) ? @dotted_errors[key] |= error : @dotted_errors[key] = error
instance_variable_set(:@dotted_errors, @dotted_errors)
end

Expand Down

0 comments on commit 4762348

Please sign in to comment.