diff options
author | shock_one <shockone89@gmail.com> | 2013-03-24 09:34:27 +0200 |
---|---|---|
committer | shock_one <shockone89@gmail.com> | 2013-03-24 09:38:28 +0200 |
commit | ec1b715b0e6f0a345b94a44b2a03b6044091a706 (patch) | |
tree | 0ef0f800d850288341a8d89e9f7efe5b3a58b5cf /activemodel/lib | |
parent | 1f1adb835a679551b23de8c18514b747ef146137 (diff) | |
download | rails-ec1b715b0e6f0a345b94a44b2a03b6044091a706.tar.gz rails-ec1b715b0e6f0a345b94a44b2a03b6044091a706.tar.bz2 rails-ec1b715b0e6f0a345b94a44b2a03b6044091a706.zip |
Add a method full_messages_for to the Errors class
Diffstat (limited to 'activemodel/lib')
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 963e52bff3..485eb1a40a 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -352,6 +352,20 @@ module ActiveModel map { |attribute, message| full_message(attribute, message) } end + # Returns all the full error messages for a given attribute in an array. + # + # class Person + # validates_presence_of :name, :email + # validates_length_of :name, in: 5..30 + # end + # + # person = Person.create() + # person.errors.full_messages_for(:name) + # # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"] + def full_messages_for(attribute) + (get(attribute) || []).map { |message| full_message(attribute, message) } + end + # Returns a full message for a given attribute. # # person.errors.full_message(:name, 'is invalid') # => "Name is invalid" |