Created
October 3, 2010 04:08
-
-
Save BinaryMuse/608256 to your computer and use it in GitHub Desktop.
lib/core_ext/rails/active_model/errors.rb - get one validation error message per invalid attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveModel | |
class Errors | |
def full_message_per_field | |
messages_per_field = [] | |
handled_attributes = [] | |
each do |attribute, messages| | |
next if handled_attributes.include? attribute | |
messages = Array.wrap(messages) | |
next if messages.empty? | |
if attribute == :base | |
messages_per_field << messages.first | |
else | |
attr_name = attribute.to_s.gsub('.', '_').humanize | |
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) | |
options = { :default => "%{attribute} %{message}", :attribute => attr_name } | |
messages_per_field << I18n.t(:"errors.format", options.merge(:message => messages.first)) | |
end | |
handled_attributes << attribute | |
end | |
messages_per_field | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment