Skip to content
This repository
Browse code

confirmation validation error attribute

This will render the error message on :#{attribute}_confirmation instead
of on attribute itself. When rendering confirmation errors inline on the
form with form builders such as SimpleForm and Formtastic it is
confusing to the ender user to see the confirmation error message on the
attribute element. Instead it makes more sense to have this validation
error render on the confirmation field instead.

The i18n message has been updated for the confirmation validator error
message to include the original attribute name.
  • Loading branch information...
commit fcc534ed76a7ef50bf7887378ce6c68d5ace336c 1 parent 82c3aca
Brian Cardarella authored April 23, 2012
2  activemodel/lib/active_model/locale/en.yml
@@ -9,7 +9,7 @@ en:
9 9
       inclusion: "is not included in the list"
10 10
       exclusion: "is reserved"
11 11
       invalid: "is invalid"
12  
-      confirmation: "doesn't match confirmation"
  12
+      confirmation: "doesn't match %{attribute}"
13 13
       accepted: "must be accepted"
14 14
       empty: "can't be empty"
15 15
       blank: "can't be blank"
2  activemodel/lib/active_model/validations/confirmation.rb
@@ -5,7 +5,7 @@ module Validations
5 5
     class ConfirmationValidator < EachValidator
6 6
       def validate_each(record, attribute, value)
7 7
         if (confirmed = record.send("#{attribute}_confirmation")) && (value != confirmed)
8  
-          record.errors.add(attribute, :confirmation, options)
  8
+          record.errors.add(:"#{attribute}_confirmation", :confirmation, options.merge(:attribute => attribute))
9 9
         end
10 10
       end
11 11
 
2  activemodel/test/cases/validations/confirmation_validation_test.rb
@@ -44,7 +44,7 @@ def test_validates_confirmation_of_for_ruby_class
44 44
     p.karma_confirmation = "None"
45 45
     assert p.invalid?
46 46
 
47  
-    assert_equal ["doesn't match confirmation"], p.errors[:karma]
  47
+    assert_equal ["doesn't match karma"], p.errors[:karma_confirmation]
48 48
 
49 49
     p.karma = "None"
50 50
     assert p.valid?
2  activemodel/test/cases/validations/i18n_generate_message_validation_test.rb
@@ -37,7 +37,7 @@ def test_generate_message_invalid_with_custom_message
37 37
 
38 38
   # validates_confirmation_of: generate_message(attr_name, :confirmation, :message => custom_message)
39 39
   def test_generate_message_confirmation_with_default_message
40  
-    assert_equal "doesn't match confirmation", @person.errors.generate_message(:title, :confirmation)
  40
+    assert_equal "doesn't match Title", @person.errors.generate_message(:title, :confirmation)
41 41
   end
42 42
 
43 43
   def test_generate_message_confirmation_with_custom_message
17  activemodel/test/cases/validations/i18n_validation_test.rb
@@ -81,7 +81,7 @@ def test_errors_full_messages_uses_format
81 81
     test "validates_confirmation_of on generated message #{name}" do
82 82
       Person.validates_confirmation_of :title, validation_options
83 83
       @person.title_confirmation = 'foo'
84  
-      @person.errors.expects(:generate_message).with(:title, :confirmation, generate_message_options)
  84
+      @person.errors.expects(:generate_message).with(:title_confirmation, :confirmation, generate_message_options.merge(:attribute => :title))
85 85
       @person.valid?
86 86
     end
87 87
   end
@@ -217,24 +217,29 @@ def test_errors_full_messages_uses_format
217 217
 
218 218
   # To make things DRY this macro is defined to define 3 tests for every validation case.
219 219
   def self.set_expectations_for_validation(validation, error_type, &block_that_sets_validation)
  220
+    if error_type == :confirmation
  221
+      attribute = :title_confirmation
  222
+    else
  223
+      attribute = :title
  224
+    end
220 225
     # test "validates_confirmation_of finds custom model key translation when blank"
221 226
     test "#{validation} finds custom model key translation when #{error_type}" do
222  
-      I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {error_type => 'custom message'}}}}}}
  227
+      I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {attribute => {error_type => 'custom message'}}}}}}
223 228
       I18n.backend.store_translations 'en', :errors => {:messages => {error_type => 'global message'}}
224 229
 
225 230
       yield(@person, {})
226 231
       @person.valid?
227  
-      assert_equal ['custom message'], @person.errors[:title]
  232
+      assert_equal ['custom message'], @person.errors[attribute]
228 233
     end
229 234
 
230 235
     # test "validates_confirmation_of finds custom model key translation with interpolation when blank"
231 236
     test "#{validation} finds custom model key translation with interpolation when #{error_type}" do
232  
-      I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {:title => {error_type => 'custom message with %{extra}'}}}}}}
  237
+      I18n.backend.store_translations 'en', :activemodel => {:errors => {:models => {:person => {:attributes => {attribute => {error_type => 'custom message with %{extra}'}}}}}}
233 238
       I18n.backend.store_translations 'en', :errors => {:messages => {error_type => 'global message'}}
234 239
 
235 240
       yield(@person, {:extra => "extra information"})
236 241
       @person.valid?
237  
-      assert_equal ['custom message with extra information'], @person.errors[:title]
  242
+      assert_equal ['custom message with extra information'], @person.errors[attribute]
238 243
     end
239 244
 
240 245
     # test "validates_confirmation_of finds global default key translation when blank"
@@ -243,7 +248,7 @@ def self.set_expectations_for_validation(validation, error_type, &block_that_set
243 248
 
244 249
       yield(@person, {})
245 250
       @person.valid?
246  
-      assert_equal ['global message'], @person.errors[:title]
  251
+      assert_equal ['global message'], @person.errors[attribute]
247 252
     end
248 253
   end
249 254
 
2  activemodel/test/cases/validations/validates_test.rb
@@ -154,6 +154,6 @@ def test_defining_extra_default_keys_for_validates
154 154
     topic.title = "What's happening"
155 155
     topic.title_confirmation = "Not this"
156 156
     assert !topic.valid?
157  
-    assert_equal ['Y U NO CONFIRM'], topic.errors[:title]
  157
+    assert_equal ['Y U NO CONFIRM'], topic.errors[:title_confirmation]
158 158
   end
159 159
 end

0 notes on commit fcc534e

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