Skip to content

Commit

Permalink
Handle error message being an array (from payrollhero#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
guanw88 committed Sep 4, 2024
1 parent 2b03a42 commit 00cf610
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/active_model/dynamic_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ def full_messages
full_messages = []

each do |error|
if error.attribute == :base
full_messages << error.message
else
attr_name = error.attribute.to_s.gsub('.', '_').humanize
attr_name = @base.class.human_attribute_name(error.attribute, :default => attr_name)
options = { :default => "%{attribute} %{message}", :attribute => attr_name }

if error.message =~ /^\^/
options[:default] = "%{message}"
full_messages << I18n.t(:"errors.dynamic_format", **options.merge(:message => error.message[1..-1]))
elsif error.message.is_a? Proc
options[:default] = "%{message}"
full_messages << I18n.t(:"errors.dynamic_format", **options.merge(:message => error.message.call(@base)))
Array.wrap(error.message).each do |error_message|
if error.attribute == :base
full_messages << error_message
else
full_messages << I18n.t(:"errors.format", **options.merge(:message => error.message))
attr_name = error.attribute.to_s.gsub('.', '_').humanize
attr_name = @base.class.human_attribute_name(error.attribute, :default => attr_name)
options = { :default => "%{attribute} %{message}", :attribute => attr_name }

if error_message =~ /^\^/
options[:default] = "%{message}"
full_messages << I18n.t(:"errors.dynamic_format", **options.merge(:message => error_message[1..-1]))
elsif error_message.is_a? Proc
options[:default] = "%{message}"
full_messages << I18n.t(:"errors.dynamic_format", **options.merge(:message => error_message.call(@base)))
else
full_messages << I18n.t(:"errors.format", **options.merge(:message => error_message))
end
end
end
end
Expand Down

0 comments on commit 00cf610

Please sign in to comment.