From ec1b715b0e6f0a345b94a44b2a03b6044091a706 Mon Sep 17 00:00:00 2001 From: shock_one <shockone89@gmail.com> Date: Sun, 24 Mar 2013 09:34:27 +0200 Subject: Add a method full_messages_for to the Errors class --- activemodel/lib/active_model/errors.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'activemodel/lib') 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" -- cgit v1.2.3