diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-03-25 09:04:25 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-03-25 09:04:25 -0700 |
commit | b8b3df0e63bf60bd38e2ed99bcb91b2d56e34a4c (patch) | |
tree | 769e000ca64624b987d3135c22bb1636d0e865aa /activemodel/lib/active_model | |
parent | ba012fa04d567438d42f37f9454c6062663e6e4f (diff) | |
parent | ec1b715b0e6f0a345b94a44b2a03b6044091a706 (diff) | |
download | rails-b8b3df0e63bf60bd38e2ed99bcb91b2d56e34a4c.tar.gz rails-b8b3df0e63bf60bd38e2ed99bcb91b2d56e34a4c.tar.bz2 rails-b8b3df0e63bf60bd38e2ed99bcb91b2d56e34a4c.zip |
Merge pull request #8527 from shockone/patch-1
Add a method full_messages_for to the Errors class
Diffstat (limited to 'activemodel/lib/active_model')
-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" |