Skip to content
This repository
Browse code

Add a method full_messages_for to the Errors class

  • Loading branch information...
commit ec1b715b0e6f0a345b94a44b2a03b6044091a706 1 parent 1f1adb8
Владимир authored March 24, 2013
5  activemodel/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,10 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Add ActiveModel::Errors#full_messages_for, a method that returns all the error
  4
+    messages for a given attribute.
  5
+
  6
+    *Volodymyr Shatsky*
  7
+
3 8
 *   Added a method so that validations can be easily cleared on a model.
4 9
     For example:
5 10
 
14  activemodel/lib/active_model/errors.rb
@@ -352,6 +352,20 @@ def full_messages
352 352
       map { |attribute, message| full_message(attribute, message) }
353 353
     end
354 354
 
  355
+    # Returns all the full error messages for a given attribute in an array.
  356
+    #
  357
+    #   class Person
  358
+    #     validates_presence_of :name, :email
  359
+    #     validates_length_of :name, in: 5..30
  360
+    #   end
  361
+    #
  362
+    #   person = Person.create()
  363
+    #   person.errors.full_messages_for(:name)
  364
+    #   # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
  365
+    def full_messages_for(attribute)
  366
+      (get(attribute) || []).map { |message| full_message(attribute, message) }
  367
+    end
  368
+
355 369
     # Returns a full message for a given attribute.
356 370
     #
357 371
     #   person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
20  activemodel/test/cases/errors_test.rb
@@ -216,6 +216,26 @@ def test_has_key?
216 216
     person.errors.add(:name, "can not be nil")
217 217
     assert_equal ["name can not be blank", "name can not be nil"], person.errors.full_messages
218 218
   end
  219
+  
  220
+  test 'full_messages_for should contain all the messages for a given attribute' do
  221
+    person = Person.new
  222
+    person.errors.add(:name, "can not be blank")
  223
+    person.errors.add(:name, "can not be nil")
  224
+    assert_equal ["name can not be blank", "name can not be nil"], person.errors.full_messages_for(:name)
  225
+  end
  226
+
  227
+  test 'full_messages_for should not contain messages for another attributes' do
  228
+    person = Person.new
  229
+    person.errors.add(:name, "can not be blank")
  230
+    person.errors.add(:email, "can not be blank")
  231
+    assert_equal ["name can not be blank"], person.errors.full_messages_for(:name)
  232
+  end
  233
+
  234
+  test "full_messages_for should return an empty array in case if errors hash doesn't contain a given attribute" do
  235
+    person = Person.new
  236
+    person.errors.add(:name, "can not be blank")
  237
+    assert_equal [], person.errors.full_messages_for(:email)
  238
+  end
219 239
 
220 240
   test 'full_message should return the given message if attribute equals :base' do
221 241
     person = Person.new

0 notes on commit ec1b715

Please sign in to comment.
Something went wrong with that request. Please try again.