Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support i18n attributes for confirmation

  • Loading branch information...
commit 4433b1a99afa904884927d6787a1baf162b14f2e 1 parent 3a749a9
@bcardarella bcardarella authored
View
3  activemodel/lib/active_model/validations/confirmation.rb
@@ -5,7 +5,8 @@ module Validations
class ConfirmationValidator < EachValidator
def validate_each(record, attribute, value)
if (confirmed = record.send("#{attribute}_confirmation")) && (value != confirmed)
- record.errors.add(:"#{attribute}_confirmation", :confirmation, options.merge(:attribute => attribute))
+ human_attribute_name = record.class.human_attribute_name(attribute)
+ record.errors.add(:"#{attribute}_confirmation", :confirmation, options.merge(:attribute => human_attribute_name))
end
end
View
21 activemodel/test/cases/validations/confirmation_validation_test.rb
@@ -44,7 +44,7 @@ def test_validates_confirmation_of_for_ruby_class
p.karma_confirmation = "None"
assert p.invalid?
- assert_equal ["doesn't match karma"], p.errors[:karma_confirmation]
+ assert_equal ["doesn't match Karma"], p.errors[:karma_confirmation]
p.karma = "None"
assert p.valid?
@@ -52,4 +52,23 @@ def test_validates_confirmation_of_for_ruby_class
Person.reset_callbacks(:validate)
end
+ def test_title_confirmation_with_i18n_attribute
+ @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend
+ I18n.load_path.clear
+ I18n.backend = I18n::Backend::Simple.new
+ I18n.backend.store_translations('en', {
+ :errors => {:messages => {:confirmation => "doesn't match %{attribute}"}},
+ :activemodel => {:attributes => {:topic => {:title => 'Test Title'}}}
+ })
+
+ Topic.validates_confirmation_of(:title)
+
+ t = Topic.new("title" => "We should be confirmed","title_confirmation" => "")
+ assert t.invalid?
+ assert_equal ["doesn't match Test Title"], t.errors[:title_confirmation]
+
+ I18n.load_path.replace @old_load_path
+ I18n.backend = @old_backend
+ end
+
end
View
2  activemodel/test/cases/validations/i18n_validation_test.rb
@@ -81,7 +81,7 @@ def test_errors_full_messages_uses_format
test "validates_confirmation_of on generated message #{name}" do
Person.validates_confirmation_of :title, validation_options
@person.title_confirmation = 'foo'
- @person.errors.expects(:generate_message).with(:title_confirmation, :confirmation, generate_message_options.merge(:attribute => :title))
+ @person.errors.expects(:generate_message).with(:title_confirmation, :confirmation, generate_message_options.merge(:attribute => 'Title'))
@person.valid?
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.