Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle error message being an array #1

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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